According to Jonathan Giles in https://forums.oracle.com/forums/message.jspa?messageID=9929460, tree item events are supposed to bubble up the tree item parent chain. The following code should then print "EXPANDED: .. " when expanding the Item 1 node, which is a child of the root node, but the listener is only notified about changes on the root node:
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.scene.Scene;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import javafx.stage.Stage;
public class TreeViewTest extends Application {
public void start(Stage stage) throws Exception {
TreeItem<String> root = new TreeItem<String>("Root Node");
TreeItem<String> item1 = new TreeItem<String>("Item 1");
root.getChildren().add(item1);
item1.getChildren().add(new TreeItem<String>("Child of Item 1"));
TreeView<String> treeView = new TreeView<String>(root);
root.expandedProperty().addListener(new ChangeListener<Boolean>() {
public void changed(ObservableValue<? extends Boolean> observableValue, Boolean aBoolean, Boolean aBoolean1) {
System.out.println("EXPANDED: " + observableValue.getValue());
}
});
Scene scene = new Scene(treeView);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.scene.Scene;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import javafx.stage.Stage;
public class TreeViewTest extends Application {
public void start(Stage stage) throws Exception {
TreeItem<String> root = new TreeItem<String>("Root Node");
TreeItem<String> item1 = new TreeItem<String>("Item 1");
root.getChildren().add(item1);
item1.getChildren().add(new TreeItem<String>("Child of Item 1"));
TreeView<String> treeView = new TreeView<String>(root);
root.expandedProperty().addListener(new ChangeListener<Boolean>() {
public void changed(ObservableValue<? extends Boolean> observableValue, Boolean aBoolean, Boolean aBoolean1) {
System.out.println("EXPANDED: " + observableValue.getValue());
}
});
Scene scene = new Scene(treeView);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}