Tree item select event in javafx2

烂漫一生 提交于 2020-01-10 17:30:13

问题


I have created the treeview in javafx2. i need to write the event for getting clicked node name.

pls let me know how to do this?


回答1:


use ChangeListener .

Sample code :

 treeView.getSelectionModel().selectedItemProperty().addListener( new ChangeListener() {

        @Override
        public void changed(ObservableValue observable, Object oldValue,
                Object newValue) {

            TreeItem<String> selectedItem = (TreeItem<String>) newValue;
            System.out.println("Selected Text : " + selectedItem.getValue());
            // do what ever you want 
        }

      });



回答2:


slightly updated version

treeView.getSelectionModel().selectedItemProperty()
            .addListener(new ChangeListener<TreeItem<String>>() {

                @Override
                public void changed(
                        ObservableValue<? extends TreeItem<String>> observable,
                        TreeItem<String> old_val, TreeItem<String> new_val) {
                    TreeItem<String> selectedItem = new_val;
                    System.out.println("Selected Text : " + selectedItem.getValue());
                    // do what ever you want
                }

            });



回答3:


Slightly more updated version, using lambdas which cleans it up a bit:

treeView.getSelectionModel()
        .selectedItemProperty()
        .addListener((observable, oldValue, newValue) -> System.out.println("Selected Text : " + newValue.getValue()));


来源:https://stackoverflow.com/questions/13857041/tree-item-select-event-in-javafx2

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!