JavaFX使用initialize方法实现Post渲染事件

时间:2014-07-03 10:55:27

标签: java javafx-2

执行initialize方法后,我需要使用一些Text消息更新控制器。这意味着UI的Post sage(由于用户需要看到出现此消息)。我见过一些类似帖子Post render event in JavaFXHow to listen for WindowEvent.WINDOW_SHOWN的解决方案。但似乎我无法用这些解决方案完成任务。

作为解决方案当我按照以下方式使用WindowEvent.WINDOW_SHOWN时,它将尝试执行我已声明的事件处理程序位置(在应用程序的早期阶段),这意味着在初始化阶段时(执行Application的开始(阶段阶段))然后我得到一些空指针异常。我的应用程序有许多控制器,我需要在一些UI流程的中间做这个。一旦执行了特定控制器的初始化方法(在后渲染阶段),我该如何完成此任务。谢谢。

@Override
public void start(Stage stage) throws Exception
{
    FXMLLoader loader = new FXMLLoader();
    Parent root = (Parent)loader.load(TestController.class.getResourceAsStream("TestView.fxml"));
    final TestController controller = (TestController)loader.getController();
    stage.addEventHandler(WindowEvent.WINDOW_SHOWN, new EventHandler<WindowEvent>()
    {
         @Override
        public void handle(WindowEvent window)
        {
            controller.handleWindowShownEvent();
        }
    }
    });
    Scene scene = new Scene(root);
    stage.setScene(scene);
    stage.show();
}

在我的应用程序中如下所示当我添加事件处理程序并使用Spring Context加载相关的Controller时,它将产生Null Pointer Exception。

使用 handleWindowShowEvent 方法,我使用了我声明的所有UI组件和资源包。当该方法将要执行时,它将使用那些UI控制器和资源包调用生成Null Pointer Exception。

问题是,由于UI尚未呈现,似乎尚未执行Initialize块,但 handleWindowShowEvent 方法将使用这些未初始化的组件。

添加事件处理程序

  stage.addEventHandler(WindowEvent.WINDOW_SHOWN, new EventHandler<WindowEvent>() {

    @Override
    public void handle(WindowEvent window) {

         Platform.runLater(new Runnable() {
            @Override
            public void run() {

              try {

                  // Load Controller with Spring Context
                  EquipmentConfigProcessController eqConfProcessController
                          = ComponentUtil.appContext.getBean(EquipmentConfigProcessController.class);

                  eqConfProcessController.handleWindowShowEvent();

              } catch (Exception e) {
                  logger.error("Exception while going to execute handleWindowShowEvent method on " +
                          "EquipmentConfigProcessController [" + e.getMessage() + "]");
              }
          }
        });
    }
});

初始化方法

      private ResourceBundle rb;
@FXML private Pane detailPane;
@FXML private HBox detailBox;
@FXML public Label devNameLbl;

@Override
public void initialize(URL url, ResourceBundle rb) {

 // Initialise resource bundle
 this.rb = rb;
}

handleWindowShowEvent方法

   public void handleWindowShowEvent() {

        // Null Pointer Exception occurred with Resource ResourceBundle 
        Label updateMessage = new Label(rb.getString("equipment.message"));

        // Null Pointer Exception occurred with detailBox invocation  
        detailBox.getChildren().clear();

        // More codes
   }

1 个答案:

答案 0 :(得分:2)

尝试

stage.addEventHandler(WindowEvent.WINDOW_SHOWN, new EventHandler<WindowEvent>() {
    @Override
    public void handle(WindowEvent window) {
        Platform.runLater(new Runnable() {
            @Override
            public void run() {
                controller.handleWindowShownEvent();
            }
        });
    }
});