RequestFocus in TextField doesn't work

后端 未结 6 1225
隐瞒了意图╮
隐瞒了意图╮ 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:16

    If you requestFocus(); after initializing the scene, it will work!

    Like this:

    Stage stage = new Stage();
    GridPane grid = new GridPane();
    //... add buttons&stuff to pane
    
    Scene scene = new Scene(grid, 800, 600);
    
    TEXTFIELD.requestFocus();
    
    stage.setScene(scene);
    stage.show();
    

    I hope this helps. :)

提交回复
热议问题