Is there a focus handler in javafx

徘徊边缘 提交于 2019-12-11 10:13:03

问题


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?


回答1:


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){
        }
    }
}



回答2:


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

scene.focusOwnerProperty().addChangeListener(...)


来源:https://stackoverflow.com/questions/17890631/is-there-a-focus-handler-in-javafx

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!