Wottensprels
Wottensprels

Reputation: 3327

JavaFx form load

I want to group some radiobuttons in a togglegroup. The group is created in the controllerclass:

final ToggleGroup rbtGroup = new ToggleGroup();

Now i create a function which adds those buttons to the group. But how do i fire the function on the so called form-load?

I was writing Visual Basic for my company in the past few months, and there is an event called load on the form. Therefore, i can handle each event (load, change etc.) by simply writing my code in the prevailing event.

How do i achieve that on Java FX?

Excuse me for this question, but i find that the Java FX documentation is very confusing.

Upvotes: 0

Views: 1916

Answers (1)

Uluk Biy
Uluk Biy

Reputation: 49215

Your controller class has to implement the javafx.fxml.Initializable interface, and override (implement) the only method initialize(). This initialize() method (there is no function in Java terminology) is a "form load function" of the loading FXML file. So you can define and initiate controls and variables in this method like:

...
@FXML
private ToggleGroup rbtGroup;

@Override
public void initialize(URL url, ResourceBundle rb) {

    rbtGroup = new ToggleGroup();

    ToggleButton tb1 = new ToggleButton("ToggleButton 1");
    tb1.setToggleGroup(rbtGroup);

    ToggleButton tb2 = new ToggleButton("ToggleButton 2");
    tb2.setToggleGroup(rbtGroup);
}
...

Note the @FXML annotation. Put it if the rbtGroup is defined in the loading FXML file.
Or call your "adding buttons to the group" method (again not function) in that initialize() method.

Upvotes: 3

Related Questions