Tree item select event in javafx2

后端 未结 3 1380
囚心锁ツ
囚心锁ツ 2020-12-31 00:52

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条回答
  • 2020-12-31 01:26

    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 
            }
    
          });
    
    0 讨论(0)
  • 2020-12-31 01:35

    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()));
    
    0 讨论(0)
  • 2020-12-31 01:40

    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
                    }
    
                });
    
    0 讨论(0)
提交回复
热议问题