I have created the following scene with the help of Scene Builder:
- I set up a BorderPane
- In the insert TOP area I have placed a MenuBar
- In the insert CENTER I have placed some TextFields
- In the insert BOTTOM area I have placed a Button
Upon opening my application, the focus is given to the Button that was placed in the insert BOTTOM section.
This is an image depicting this specific scenario
What can I do to set the focus to the first TextField in the insert CENTER section?
I have tried the following in order to solve this issue:
- I made sure that the Hierarchy order was correct (i.e. the center node comes before the bottom node).
- I tried using
.requestFocus()
within myinitialize()
declaration but that did not work sinceinitialize()
occurs before loading the controller as described in JavaFx:What if I want to do something after initialize(),before the scene show,how can I achieve this?. - Another option I looked into was @Uluk Biy's answer in this question: JavaFX: Focusing textfield programmatically. However, in my application I have two controllers (main_controller.java and sample_app_controller). The main_controller.java is responsible for launching the application, meaning that it has the
public static void main(String[] args) {launch(args));}
andpublic void start(Stage primaryStage)
functions through which my application runs. sample_app_controller is responsible of handling anything that happens in the second screen (the one I provided in this post). I understand that I can place the code provided by @Uluk Biy in mypublic void start(Stage primaryStage
function. However, at that point I am in a different screen which is controlled by a different java file, so I can't really apply that solution.