focusjavafxfocusmanager

Is there a focus handler in javafx


In swing is a FocusManager available to get notified if the focus changes.

FocusManager.getCurrentManager().addPropertyChangeListener (...)

Is there an analogue way in javafx to get notified if the focus in the scenegraph changes?


Solution

  • There's none yet but you can try manually looping among the focusedProperties of your target nodes

    private void handleFocusChangesStartingFromParentNode(Parent parentNode) {
    
        for (Node node : parentNode.getChildrenUnmodifiable()) {
            node.focusedProperty().addListener(new ChangeListener<Boolean>() {
                @Override
                public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
                    performHandling();
                }
            });
            try{
                handleFocusChangesStartingFromNode((Parent)node);
            }catch(ClassCastException e){
            }
        }
    }