Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tree item select event in javafx2

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?

like image 452
velsachin Avatar asked Dec 13 '12 09:12

velsachin


3 Answers

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          }        }); 
like image 190
invariant Avatar answered Sep 22 '22 13:09

invariant


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())); 
like image 25
Evan Knowles Avatar answered Sep 22 '22 13:09

Evan Knowles


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
                }

            });
like image 37
MitchBroadhead Avatar answered Sep 22 '22 13:09

MitchBroadhead