Tree item select event in javafx2


Question

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
17
12/13/2012 9:44:02 AM

Accepted Answer

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 
        }

      });
40
12/13/2012 1:04:02 PM

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
                }

            });

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon