RequestFocus in TextField doesn't work

后端 未结 6 723
猫巷女王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:36

    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. :)

提交回复
热议问题