JavaFXFXML 控制器构造函数与初始化方法

我的 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);
}
}

通过先调用缺省构造函数,然后调用 initialize方法,FXMLLoader创建了相应控制器的一个实例(通过 fx:controllerFXML文件中给出) :

public class MainViewController {


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


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

输出结果是:

first
second

那么,为什么存在 initialize方法呢?使用构造函数或 initialize方法初始化控制器所需的内容之间有什么区别?

谢谢你的建议!

172814 次浏览

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:

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.
}
}

In a few words: The constructor is called first, then any @FXML annotated fields are populated, then initialize() is called.

This means the constructor does not have access to @FXML fields referring to components defined in the .fxml file, while initialize() does have access to them.

Quoting from the Introduction to 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.

In Addition to the above answers, there probably should be noted that there is a legacy way to implement the initialization. There is an interface called Initializable from the fxml library.

import javafx.fxml.Initializable;


class MyController implements Initializable {
@FXML private TableView<MyModel> tableView;


@Override
public void initialize(URL location, ResourceBundle resources) {
tableView.getItems().addAll(getDataFromSource());
}
}

Parameters:

location - The location used to resolve relative paths for the root object, or null if the location is not known.
resources - The resources used to localize the root object, or null if the root object was not localized.

And the note of the docs why the simple way of using @FXML public void initialize() works:

NOTE This interface has been superseded by automatic injection of location and resources properties into the controller. FXMLLoader will now automatically call any suitably annotated no-arg initialize() method defined by the controller. It is recommended that the injection approach be used whenever possible.