RequestFocus in TextField doesn't work

后端 未结 6 725
猫巷女王i
猫巷女王i 2021-02-13 01:49

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:20

    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.

提交回复
热议问题