user5182503
user5182503

Reputation:

Event if some node in tree of nodes gets focus

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

Answers (1)

DVarga
DVarga

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

Related Questions