JavaFX FXML controller - constructor vs initialize method

后端 未结 3 551
忘了有多久
忘了有多久 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 13:02

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

提交回复
热议问题