如何在javafx8中捕获TableView创建



我有一个带有TableView的JavaFX应用程序,一旦应用程序启动,我需要填充数据。我以以下方式启动应用程序:

private LayoutController theController;
@Override
public void start(Stage primaryStage)
{
    try {
     FXMLLoader fxmlload = new FXMLLoader(getClass().getResource("Sample.fxml"));
     BorderPane root = (BorderPane )fxmlload.load();
        Scene scene = new Scene(root,640,480);
        scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
     theController = (LayoutController )fxmlload.getController();
     primaryStage.setTitle("Title Application");
     primaryStage.addEventHandler(WindowEvent.WINDOW_SHOWN,theController.windowStarted);
        primaryStage.setScene(scene);
        primaryStage.show();
    } catch(Exception e) {
        e.printStackTrace();
    }
}

在我的控制器SampleController中,我有TableView对象所以它(最初)创建了一些列,一旦它启动

@FXML Parent myRoot;
@FXML TableView datTable<DataClass>;
private Stage theStage;
public EventHandler<WindowEvent> windowStarted = event -> {
  theStage = (Stage )myRoot.getScene().getWindow();
  getData();
};
protected void getData()
{
    dataTable.setEditable(false);
          .
          // Call a SOAP service to get the data
          .
}

我假设一旦Stage的window_show事件发生,控件就被创建了,我可以对它们做一些事情。显然情况并非如此。显然,使用FXML指定的控件实际上是在主应用程序窗口创建之后的某个时间创建的!

发生的情况是,当执行windowStarted lambda时,调用getData()方法,但显然在window_show事件发生之前没有创建dataTable。因此,当我尝试调用任何dataTable的方法时,我得到NullPointerException失败!

我需要捕捉dataTable实际创建的时间,以便我可以使用它的方法。有什么办法可以做到吗?

将数据下载的代码放在initialize方法中,该方法在控制器的根元素被完全处理后调用,或者换句话说,在所有FXML字段被分配后。

@FXML
public void initialize() {
    //Here!
}

最新更新