Reputation:
Let's suppose we have a tree of nodes, something like:
VBox
|
/ \
Pane SplitPane
/ \
HBox Pane
/ \ |
Button Button TextField
What is the optimal way (without setting listener on every node) to get event when any node of this tree gets focus?
Upvotes: 1
Views: 109
Reputation: 21799
You can use for example the focusOwnerProperty
of the Scene
:
The scene's current focus owner node.
scene.focusOwnerProperty().addListener((obs, oldval, newval) ->
System.out.println(checkFocus(vbox)));
where checkFocus
checks recursively the focusedProperty
of the nodes originated from the VBox
:
private boolean checkFocus(Parent p) {
if (!p.isFocused()) {
for (Node node :p.getChildrenUnmodifiable()) {
if (node instanceof Parent && checkFocus((Parent) node))
return true;
else if (node.isFocused())
return true;
}
} else
return true;
return false;
}
Upvotes: 0