Tree item select event in javafx2

velsachin picture velsachin · Dec 13, 2012 · Viewed 32.9k times · Source

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?

Answer

invariant picture invariant · Dec 13, 2012

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 
        }

      });