1

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:

  1. I made sure that the Hierarchy order was correct (i.e. the center node comes before the bottom node).
  2. I tried using .requestFocus() within my initialize() declaration but that did not work since initialize() 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?.
  3. 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));} and public 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 my public 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.
El Fufu
  • 66
  • 1
  • 9
  • related: https://stackoverflow.com/questions/15238928/javafx-how-to-change-the-focus-traversal-policy – fabian Nov 04 '17 at 10:11

0 Answers0