简体   繁体   中英

Access treeItem, not treeView

I would like to access the treeItems in my treeView via mouse clicked. I have no problem to access the treeview because I can call treeView.setOnMouseClicked, but the treeItem doesn't have that method. I have tried a few things like tried EventHandler, but can't get it to work:

    TreeItem<String> soc1Root = new TreeItem<String>("Bla - 1");
    soc1Root.setExpanded(true);

    TreeItem<String> soc1 = new TreeItem<String>("UnderBla - 1");
    soc1.setExpanded(true);
    soc1Root.getChildren().add(soc1);

    TreeItem<String> termisk = new TreeItem<String>("Stuff happen when clicked!");
    soc1.getChildren().add(termisk);

    socialKval = new TreeView<String>(soc1Root);

    pane.setLeft(socialKval);

    EventHandler<MouseEvent> mouseEventHandle = (MouseEvent event) -> {
            getTermisk(pane);
            System.out.println("Termisk - test");
        };

        termisk.addEventHandler(MouseEvent.MOUSE_CLICKED, mouseEventHandle);

Simply listen to the selection model's selected item:

treeview.getSelectionModel().getSelectedItem();

OR

treeview.getSelectionModel().selectedItemProperty();

Happy Coding,
Kalasch

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM