Adil Adil - 3 months ago 45
Java Question

RequestFocus in TextField doesn't work -JavaFX 2.1

I'm using JavaFX 2.1 and I'have created GUI using FXML, in the controller of this GUI I have added

myTextField.requestFocus();
, but always I get the focus in the first button that I have added to the GUI. I'm using Scene Builder 1.0.

Answer

At the time of initialize() controls are not yet ready to handle focus.

You can try next trick:

@Override
public void initialize(URL url, ResourceBundle rb) {
    Platform.runLater(new Runnable() {
        @Override
        public void run() {
            tf.requestFocus();
        }
    });
}
Comments