Is there a focus handler in javafx

2019-08-13 08:14发布

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?

2条回答
放我归山
2楼-- · 2019-08-13 08:24

You can add a ChangeListener to the focusOwner property of a Scene now:

scene.focusOwnerProperty().addChangeListener(...)
查看更多
beautiful°
3楼-- · 2019-08-13 08:31

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){
        }
    }
}
查看更多
登录 后发表回答