How to initialize member variables of a custom javafx controller?


Question

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?

1
4
12/3/2013 1:41:03 AM

Accepted Answer

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

9
3/28/2014 1:03:25 PM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon