Javafx Treeview item action event

27,483

Solution 1

According to the JavaFX 2.2 documentation :

" ..a TreeItem is not a Node, and therefore no visual events will be fired on the TreeItem, To get these events, it is necessary to add relevant observers to the TreeCell instances (via a custom cell factory)."

I think this example on using TreeView will be somehow useful.

Solution 2

This may be solved by implementing CellFactory, but I think the easiest way is like this:

1) Create and add an event handler to the TreeView:

EventHandler<MouseEvent> mouseEventHandle = (MouseEvent event) -> {
    handleMouseClicked(event);
};

treeView.addEventHandler(MouseEvent.MOUSE_CLICKED, mouseEventHandle); 

2) Handle only clicks on the nodes (and not on empy space os the TreeView):

private void handleMouseClicked(MouseEvent event) {
    Node node = event.getPickResult().getIntersectedNode();
    // Accept clicks only on node cells, and not on empty spaces of the TreeView
    if (node instanceof Text || (node instanceof TreeCell && ((TreeCell) node).getText() != null)) {
        String name = (String) ((TreeItem)treeView.getSelectionModel().getSelectedItem()).getValue();
        System.out.println("Node click: " + name);
    }
}

Solution 3

I couldn't find method getPickResult in mouse event, so maybe next is preferable then answer from Alex:

1) add listener to tree view

treeView.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> handle(newValue));

2) handle clicks, it's not need distinguish clicks on empty space and nodes

private void updateSelectedItem(Object newValue) {      
    System.out.println(newValue);
}
Share:
27,483
Marc Rasmussen
Author by

Marc Rasmussen

SOreadytohelp Website

Updated on July 05, 2022

Comments

  • Marc Rasmussen
    Marc Rasmussen almost 2 years

    I'm trying to create a menu using a treeView. This is the first time I'm using treeView and have been reading up on it on several websites.

    I'm having some problems when it comes to action event. What I want to do is basically to fire and event when ever the user clicks a node in the treeview so far I have the following:

            TreeItem<String> rootItem = new TreeItem<String>("Navigation");
        TreeItem<String> statistics = new TreeItem<String>("Statistics");
        TreeItem<String> clan = new TreeItem<String>("Clan page");
        clan.addEventHandler(MouseEvent, new EventHandler<MouseEvent>() {
    
            @Override
            public void handle(MouseEvent event) {
                // TODO Auto-generated method stub
    
            }
        });
    
        rootItem.getChildren().add(statistics);
        rootItem.getChildren().add(clan);
    
        TreeView<String> tree = new TreeView<String>(rootItem); 
    

    Sadly this doesn't seem to work.

    Is there any way I can add a clicklistener or even an actionlistener to the individual items in my treeView without changing the treeItems to type Button ?

  • kinsley kajiva
    kinsley kajiva over 6 years
    this works but just change updateSelectedItem() to handle() or whatever you want but this works. this should be the correct answer!