Javafx - Can application class be the controller class

后端 未结 2 1653
闹比i
闹比i 2020-11-22 08:04

I\'m currently teaching myself JavaFX, and I\'ve taken a simple example program that\'s hardcoded the view and am turning it into one that uses FXML (mostly so I can use Sce

2条回答
  •  孤独总比滥情好
    2020-11-22 08:10

    If you have defined your application class to be the controller in the FXML file, JavaFX will, if I remember correctly, create a new instance of your application class and use the new instance as a controller. Thus, your existing application class still has null for the table.

    You can however define the controller programmatically in your application class to use your own instance:

    FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("example.fxml"));
    fxmlLoader.setController(this);
    Parent root = (Parent)fxmlLoader.load();
    

提交回复
热议问题