How does FXMLLoader load the FXML's controller?

前端 未结 2 1797
长情又很酷
长情又很酷 2021-01-12 11:14

What happens when I call FXMLLoader#load() in JavaFX?

Suppose the FXML controller extends a class that has a constructor. Will there be assurance that the construct

2条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-12 11:41

    To answer your question "what does a FXMLLoader do exactly when building a controller?":

    first it will try to get a controller instance:

    • if there is already a controller instance set on the FXMLLoader, it will use this instance.
    • if there is no controller factory set on the FXMLLoader, it tries to call a zero-argument-constructor of the class per reflection. If this fails, an InstantiationException is thrown.
    • if there is a controller factory set, it will call this factory to create a new controller instance.

    after getting the controller instance, it will do the following with it:

    • if this class implements Initializable, it calls public void initialize(URL url, ResourceBundle resourceBundle). If not, it looks if there is a method called initialize with zero arguments and calls it via reflection. If not, it does nothing.

提交回复
热议问题