Equivalent to JavaFX 2.2 onLoad

I am looking for an EventListener or method that will run when the FXML file is loaded.

Is there something similar to Javascript onLoad in JavaFX?

I just want to run a method that will clear any data from TextFields.

+3
source share
1 answer

Calling code when loading FXML

In the controller class, define a method:

@FXML
protected void initialize(URL location, Resources resources) 

This method will automatically be called by FXMLLoader when the FXML file is loaded.

There is a sample in Introduction to FXML (I just reproduced it here, slightly modified).

FXML

<VBox fx:controller="com.foo.MyController"
    xmlns:fx="http://javafx.com/fxml">
    <children>
        <Button fx:id="button" text="Click Me!"/>
    </children>
</VBox>

Java

package com.foo;

public class MyController implements Initializable {
    @FXML private Button button;

    @FXML
    protected void initialize(URL location, Resources resources) {
        button.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                System.out.println("You clicked me!");
            }
        });
    }
}

initialize () method without location and URL

, initialize , , , , Initializable , FXML :

public void initialize() 

.:

,

, FXML, FXML ( , FXMLLoader FXMLLoader , , , ). , " , TextFields", , FXML ( , ).

+11

All Articles