RequestFocus in TextField doesn't work

后端 未结 6 1243
隐瞒了意图╮
隐瞒了意图╮ 2021-02-13 02:11

I use JavaFX 2.1 and I created GUI using FXML, in the controller of this GUI I added myTextField.requestFocus();.

But I always get the focus in the other co

6条回答
  •  后悔当初
    2021-02-13 02:18

    I ran into the same problem using JavaFX 11 and solved it in a similar way that nickthecoder proposed.

    ChangeListener sceneListener = new ChangeListener() {
        @Override
        public void changed(ObservableValue observable, Scene oldValue, Scene newValue) {
            if (newValue != null) {
                editInput.requestFocus();
                editInput.sceneProperty().removeListener(this);
            }
        }
    };
    editInput.sceneProperty().addListener(sceneListener);
    

    Basicly just add a listener to the sceneProperty of the node and in that listener request focus once the scene is set. I also wrote it in such a way that the listener will be removed after it is invoked.

提交回复
热议问题