3 Replies Latest reply: Mar 30, 2013 8:43 AM by shakir.gusaroff RSS

    Select a CustomMenuItem in a ContextMenu

    drenda81
      Hi,
      I've made a small widget, a suggestbox that show result hints during typing in a textfield. It works fine but i can't select automatically the first result from the list in the context menu.
      Only if I select if with mouse I can. I would like instead to select an item programatically.

      I try to do something this:
      CustomMenuItem menuItem = new CustomMenuItem(hBox, true);
      contextMenu.getItems().add(menuItem);     
      ...
      ...
      //at the end
      contextMenu.requestFocus();
      menuItem.getContent().requestFocus();
      With above code I can see that the item is selected but really the control is not over the node, seems that it has not the focus.

      I don't understand why make a so easy action seems so hard.

      THanks!
        • 1. Re: Select a CustomMenuItem in a ContextMenu
          shakir.gusaroff
          How about this:
                final TextField textField = new TextField();
                  textField.setContextMenu(contextMenu);
          
                  textField.setOnContextMenuRequested(new EventHandler<ContextMenuEvent>() {
                      @Override
                      public void handle(ContextMenuEvent event) {
                          textField.setText(contextMenu.getItems().get(0).getText());
          
                      }
                  });
          Or this:
              
                  contextMenu.setOnShowing(new EventHandler<WindowEvent>() {
                      public void handle(WindowEvent e) {
                          textField.setText(contextMenu.getItems().get(0).getText());
                      }
                  });
          • 2. Re: Select a CustomMenuItem in a ContextMenu
            drenda81
            Hi shakir,
            thanks for your reply but I want only select the item; I don't want to populate textbox. Infact I populate the textbox in the onAnction of MenuItem and this is fine.
            My problem is how to select programatically an item of a contextMenu.

            Thanks!
            • 3. Re: Select a CustomMenuItem in a ContextMenu
              shakir.gusaroff
              Try a CustomMenuItem with a listview:
                 
                  @Override
                  public void start(Stage primaryStage) {
                     
                      final ContextMenu contextMenu = new ContextMenu();
              
                    final  ListView<String> list = new ListView<String>();
                      ObservableList<String> items = FXCollections.observableArrayList(
                              "Single", "Double", "Suite", "Family App");
                      list.setItems(items);
              
              
                      CustomMenuItem item1 = new CustomMenuItem(list);
                      contextMenu.getItems().addAll(item1);
              
                      final TextField textField = new TextField();
                      textField.setContextMenu(contextMenu);
              
                      textField.setOnContextMenuRequested(new EventHandler<ContextMenuEvent>() {
                          @Override
                          public void handle(ContextMenuEvent event) {
                              list.getSelectionModel().select(2);
              
                          }
                      });
              
                      StackPane root = new StackPane();
                      root.getChildren().add(textField);
              
                      Scene scene = new Scene(root, 300, 250);
              
                      primaryStage.setTitle("Hello World!");
                      primaryStage.setScene(scene);
                      primaryStage.show();
                  }