JavaFX FXML控制器 - 构造函数与初始化方法

时间:2022-12-05 17:01:03

My Application class looks like this:

我的Application类看起来像这样:

public class Test extends Application {

    private static Logger logger = LogManager.getRootLogger();

    @Override
    public void start(Stage primaryStage) throws Exception {

        String resourcePath = "/resources/fxml/MainView.fxml";
        URL location = getClass().getResource(resourcePath);
        FXMLLoader fxmlLoader = new FXMLLoader(location);

        Scene scene = new Scene(fxmlLoader.load(), 500, 500);

        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

The FXMLLoader creates an instance of the corresponding controller (given in the FXML file via fx:controller) by invoking first the default constructor and then the initialize method:

FXMLLoader通过首先调用默认构造函数然后调用initialize方法来创建相应控制器的实例(通过fx:controller在FXML文件中给出):

public class MainViewController {

    public MainViewController() {
        System.out.println("first");
    }

    @FXML
    public void initialize() {
        System.out.println("second");
    }
}

The output is:

输出是:

first
second

So, why does the initialize method exist? What is the difference between using a constructor or the initialize method to initialize the controller required things?

那么,为什么初始化方法存在?使用构造函数或初始化方法初始化控制器所需的东西有什么区别?

Thanks for your suggestions!

谢谢你的建议!

2 个解决方案

#1


69  

In a few words: The constructor is called first, then any @FXML annotated fields are populated, then initialize() is called. So the constructor does NOT have access to @FXML fields referring to components defined in the .fxml file, while initialize() does have access to them.

简而言之:首先调用构造函数,然后填充任何@FXML注释字段,然后调用initialize()。因此构造函数无法访问引用.fxml文件中定义的组件的@FXML字段,而initialize()确实可以访问它们。

Quoting from the Introduction to FXML:

引自FXML简介:

[...] the controller can define an initialize() method, which will be called once on an implementing controller when the contents of its associated document have been completely loaded [...] This allows the implementing class to perform any necessary post-processing on the content.

[...]控制器可以定义一个initialize()方法,当它的相关文档的内容已经完全加载时,它将在实现控制器上调用一次[...]这允许实现类执行任何必要的帖子 - 处理内容。

#2


58  

The initialize method is called after all @FXML annotated members have been injected. Suppose you have a table view you want to populate with data:

在注入所有@FXML注释成员之后调用initialize方法。假设您有一个要用数据填充的表视图:

class MyController { 
    @FXML
    TableView<MyModel> tableView; 

    public MyController() {
        tableView.getItems().addAll(getDataFromSource()); // results in NullPointerException, as tableView is null at this point. 
    }

    @FXML
    public void initialize() {
        tableView.getItems().addAll(getDataFromSource()); // Perfectly Ok here, as FXMLLoader already populated all @FXML annotated members. 
    }
}

#1


69  

In a few words: The constructor is called first, then any @FXML annotated fields are populated, then initialize() is called. So the constructor does NOT have access to @FXML fields referring to components defined in the .fxml file, while initialize() does have access to them.

简而言之:首先调用构造函数,然后填充任何@FXML注释字段,然后调用initialize()。因此构造函数无法访问引用.fxml文件中定义的组件的@FXML字段,而initialize()确实可以访问它们。

Quoting from the Introduction to FXML:

引自FXML简介:

[...] the controller can define an initialize() method, which will be called once on an implementing controller when the contents of its associated document have been completely loaded [...] This allows the implementing class to perform any necessary post-processing on the content.

[...]控制器可以定义一个initialize()方法,当它的相关文档的内容已经完全加载时,它将在实现控制器上调用一次[...]这允许实现类执行任何必要的帖子 - 处理内容。

#2


58  

The initialize method is called after all @FXML annotated members have been injected. Suppose you have a table view you want to populate with data:

在注入所有@FXML注释成员之后调用initialize方法。假设您有一个要用数据填充的表视图:

class MyController { 
    @FXML
    TableView<MyModel> tableView; 

    public MyController() {
        tableView.getItems().addAll(getDataFromSource()); // results in NullPointerException, as tableView is null at this point. 
    }

    @FXML
    public void initialize() {
        tableView.getItems().addAll(getDataFromSource()); // Perfectly Ok here, as FXMLLoader already populated all @FXML annotated members. 
    }
}