Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JAVAFX Tree view with different context menu

I want to create context menu for treeitems in a treeview. The thing is I want to display different context menu for each treeItem. How to implement this? enter image description here

Foe example I want to create "Add Employee" for Acc Dept and "Add Supporter" for IT support.

Based on name of the treeitem the context menu needs to be displayed.

like image 807
Pravin K Avatar asked Nov 23 '25 19:11

Pravin K


1 Answers

 public TreeModel() {
        MenuItem addMenuItem = new MenuItem("Create Tab");
        addMenu.getItems().add(addMenuItem);

        addMenuItem.setOnAction(new EventHandler() {
            @Override
            public void handle(Event t) {
                TreeItem newEmployee = 
                    new TreeItem<>("New Tab");
                        getTreeItem().getChildren().add(newEmployee);
            }
        });

        contextMenuProperty().bind(
  Bindings.when(Bindings.equal(itemProperty(),"TABS"))
  .then(addMenu)
  .otherwise((ContextMenu)null));



    }

This works. @James thanks a lot for your excellent article :)

like image 91
Pravin K Avatar answered Nov 28 '25 17:11

Pravin K