JavaFX FXML controller - constructor vs initialize method

后端 未结 3 547
忘了有多久
忘了有多久 2020-11-22 12:33

My Application class looks like this:

public class Test extends Application {

    private static Logger logger = LogManager.getRootLogger();

          


        
3条回答
  •  青春惊慌失措
    2020-11-22 12:50

    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.

提交回复
热议问题