Tree item select event in javafx2

2019-02-08 03:01发布

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?

3条回答
倾城 Initia
2楼-- · 2019-02-08 03:56

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楼-- · 2019-02-08 04:01

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()));
查看更多
我只想做你的唯一
4楼-- · 2019-02-08 04:05

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 
        }

      });
查看更多
登录 后发表回答