Pravin K
Pravin K

Reputation: 311

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.

Upvotes: 3

Views: 3227

Answers (1)

Pravin K
Pravin K

Reputation: 311

 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 :)

Upvotes: 2

Related Questions