Reputation: 1217
In JavaFX TreeView, is there an event similar to "BeforeTreeItemSelectionChanged"? I would like to save some settings on the old TreeItem before a new TreeItem is clicked. Thanks.
Upvotes: 1
Views: 120
Reputation: 21799
If you just want to have a reference to the previously selected item you can add a ChangeListener
to listen to the change of the selectedItemProperty
of the selection model of the TreeView
as it's changed
method passes the previous value for you:
changed(ObservableValue<? extends T> observable, T oldValue, T newValue)
This example prints the value of the previously selected item on selection:
treeView.getSelectionModel().selectedItemProperty().addListener((obs, oldVal, newVal) -> {
if(oldVal != null)
System.out.println(oldVal.getValue());
});
And this is the same, but using anonymous class to see the types:
treeView.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<TreeItem<String>>() {
@Override
public void changed(ObservableValue<? extends TreeItem<String>> observable, TreeItem<String> oldValue, TreeItem<String> newValue) {
if(oldValue != null)
System.out.println(oldValue.getValue());
}
});
Upvotes: 3