Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to initialize member variables of a custom javafx controller?

In the Spring framework, i can use the configuration files to load member variables of a class. Is there a way to do this in javafx with a custom controller or a custom object?

like image 571
j will Avatar asked Jan 12 '23 19:01

j will


1 Answers

The @FXML annotation enables the JavaFX objects whose names you defined (fx:id) to have their references reflectively injected into nonpublic fields in the controller object as the scene graph is loaded from the fxml markup.

You can accomplish something very similar to what you are requesting by defining the values that you want set as class variables in your controller object's class, and then setting the appropriate object properties programmatically (rather than in markup) in the initialize() method of your controller object.

The initialize() method is called (if it is present) after the loading of the scene graph is complete (so all the GUI objects will have been instantiated) but before control has returned to your application's invoking code.

Edit

You can use @FXML only in Controller which is specifically set in fxml file and only for fields of that class.

This is required because these fields would be initialized automatically during creation of that class' object.

 public class MyController implements Initializable{

      @FXML
      Button startButton;

      void initialize(java.net.URL location, java.util.ResourceBundle resources) {
           startButton.addActionLisetner(...);
      }

 }

Detailed tutorial is here

like image 123
Imran Avatar answered Jan 22 '23 19:01

Imran