1 Reply Latest reply: Jul 5, 2013 6:53 AM by 854dea44-c90c-4098-8b36-0f9a57938318 RSS

    Accelerator on treeview contextMenu

    854dea44-c90c-4098-8b36-0f9a57938318

      Hi,

       

      I would like to create some shortcuts on contextual action in a treeView. I use for that a CellFactory and create on the treeCell a contextMenu with menuItem and accelerator. The shortcut is well displayed in the menu but the action is not called when i press the corresponding keys.

       

      Here is an example of a try :

       

      public class TreeViewShortCut extends Application {
      
          public class DnDCell extends TreeCell<Integer> {
      
            @Override
            protected void updateItem(Integer item, boolean empty) {
              super.updateItem(item, empty);
              String text = (item == null) ? null : item.toString();
              setText(text);
              
              ContextMenu menu = new ContextMenu();
              
              menu.getItems().add(MenuItemBuilder.create()
                  .text("T_est")
                  .accelerator((KeyCombination.keyCombination("Ctrl+A")))
                  .mnemonicParsing(true)
                  .onAction(new EventHandler<ActionEvent>() {
                    @Override public void handle(ActionEvent e) {
                      System.out.println("Ctrl A accelerator detected");
                    }
                  })
                  .build());
              setContextMenu(menu);
            }
          }
      
      
          @Override
          public void start(Stage primaryStage) {
            TreeItem<Integer> treeRoot = new TreeItem(0);
            treeRoot.getChildren().add(new TreeItem(1));
            treeRoot.getChildren().add(new TreeItem(2));
            treeRoot.getChildren().add(new TreeItem(3));
            TreeView<Integer> treeView = TreeViewBuilder.<Integer>create().root(treeRoot).build();
            treeView.setCellFactory(new Callback<TreeView<Integer>, TreeCell<Integer>>() {
              @Override
              public TreeCell call(TreeView<Integer> param) {
                return new DnDCell();
              }
            });
            
            AnchorPane.setTopAnchor(treeView, 0d);
            AnchorPane.setRightAnchor(treeView, 0d);
            AnchorPane.setBottomAnchor(treeView, 0d);
            AnchorPane.setLeftAnchor(treeView, 0d);
            //
            AnchorPane root = AnchorPaneBuilder.create().children(treeView).build();
            Scene scene = new Scene(root, 300, 250);
            //
            primaryStage.setTitle("Hello World!");
            primaryStage.setScene(scene);
            primaryStage.show();
          }
      
          /**
           * The main() method is ignored in correctly deployed JavaFX 
           * application. main() serves only as fallback in case the 
           * application can not be launched through deployment artifacts,
           * e.g., in IDEs with limited FX support. NetBeans ignores main().
           * @param args the command line arguments
           */
          public static void main(String[] args) {
            launch(args);
          }
        }
      

       

      I try also to add KeyPressed EventHandler and EventFilter on the treecell but it seems keyEvent are not detected. I think this is why it does not work. How can i activate it?

       

      Thanks

        • 1. Re: Accelerator on treeview contextMenu
          854dea44-c90c-4098-8b36-0f9a57938318

          It seems accelerator does not work on contextMenu. I tried with the following code :

           

          public class MenuExample extends Application {
          
          
            /**
             * @param args the command line arguments
             */
            public static void main(String[] args) {
              launch(args);
            }
          
          
            @Override
            public void start(Stage primaryStage) {
              primaryStage.setTitle("java-buddy.blogspot.com");
              VBox root = new VBox();
              Scene scene = new Scene(root, 400, 300, Color.WHITE);
          
          
              //Top Menu Bar
              MenuBar menuBar = new MenuBar();
          
          
              Menu menu1 = new Menu("Menu");
          
          
              MenuItem menuItemA = new MenuItem("Item A");
              menuItemA.setAccelerator(KeyCombination.keyCombination("Ctrl+A"));
              menuItemA.setOnAction(new EventHandler<ActionEvent>() {
                @Override public void handle(ActionEvent e) {
                  System.out.println("Item A Clicked");
                }
              });
          
          
              MenuItem menuItemB = new MenuItem("Item B");
              menuItemB.setAccelerator(KeyCombination.keyCombination("Ctrl+B"));
              menuItemB.setOnAction(new EventHandler<ActionEvent>() {
                @Override public void handle(ActionEvent e) {
                  System.out.println("Item B Clicked");
                }
              });
          
          
              MenuItem menuItemC = new MenuItem("Item C");
              menuItemC.setAccelerator(KeyCombination.keyCombination("Ctrl+C"));
              menuItemC.setOnAction(new EventHandler<ActionEvent>() {
                @Override public void handle(ActionEvent e) {
                  System.out.println("Item C Clicked");
                }
              });
          
          
              menu1.getItems().add(menuItemA);
              menu1.getItems().add(menuItemB);
              menu1.getItems().add(menuItemC);
              menuBar.getMenus().add(menu1);
          
          
              menuBar.prefWidthProperty().bind(primaryStage.widthProperty());
          
          
              Label label = new Label("My Text");
          
          
              ContextMenu ctxMenu = new ContextMenu();
          
          
              ctxMenu.getItems().add(MenuItemBuilder.create()
                  .text("C_ontext")
                  .accelerator((KeyCombination.keyCombination("Ctrl+E")))
                  .mnemonicParsing(true)
                  .onAction(new EventHandler<ActionEvent>() {
                    @Override public void handle(ActionEvent e) {
                      System.out.println("Context Clicked");
                    }
                  })
                  .build());
          
          
              label.setContextMenu(ctxMenu);
             
              root.getChildren().addAll(menuBar, label);
              primaryStage.setScene(scene);
              primaryStage.show();
            }
          }