1

I need to update controller with some Text messages as soon as the initialize method was executed. That mean Post sage of the UI (due to user need to see appearing this messages). I have seen some solution with similar post Post render event in JavaFX , How to listen for WindowEvent.WINDOW_SHOWN. But it seems I can not accomplish my task with those solutions.

As a solution When I used WindowEvent.WINDOW_SHOWN as following manner, it will try to execute this event handler place which I have declared (with the early stage of the application) that mean when the Stage is initialized (with execution of Application's start(Stage stage)) then I got some Null Pointer Exceptions. My application have number of Controllers and I need to make this while middle of some UI flow. How can I accomplish this task as soon as the particular controller's initialize method was executed (on post render stage). Thanks.

@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();
}

As following in my application When I add the Event Handler and load the relevant Controller with Spring Context it will produce Null Pointer Exception.

With the handleWindowShowEvent method I use all the UI components and resource bundle I have declared. When the method is going to execute it will generate Null Pointer Exception with those UI controllers and resource bundle invocations.

Problem is, due to UI was not yet rendered it seems Initialize block was not executed yet but handleWindowShowEvent method going to use those uninitialized components.

Add the Event Handler

  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() + "]");
              }
          }
        });
    }
});

Initialize Method

      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 Method

   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
   }
Community
  • 1
  • 1
Channa
  • 4,963
  • 14
  • 65
  • 97
  • 1
    The fields annotated with "@FXML" are initialized with and associated with their related nodes from FXML file by FXMLLoader. The Controller's initialize method also called by FXMLLoader with appropriate parameters (URL and resource bundle). In short load your FXML files with FXMLLoader and your controller class will be instantiated by the FXMLLoader. – Uluk Biy Jul 07 '14 at 18:41

1 Answers1

3

Try as

stage.addEventHandler(WindowEvent.WINDOW_SHOWN, new EventHandler<WindowEvent>() {
    @Override
    public void handle(WindowEvent window) {
        Platform.runLater(new Runnable() {
            @Override
            public void run() {
                controller.handleWindowShownEvent();
            }
        });
    }
});
Uluk Biy
  • 48,655
  • 13
  • 146
  • 153
  • Dear Biy, Many thanks for your worth full feedback. Even that I declared that Event Handler way as you have mentioned, it still getting some Null Pointer Exception know :( . That mean this controller bind with some FXML page therefore will produce null pointer exceptions with "@FML private Label myLabel" like that entries. It seems even I loaded controller it was not initialized those UI components know. Even I denoted as "Platform.runLater" it still execute this event handler place which I have declared know. Thanks a lot. – Channa Jul 04 '14 at 01:50
  • @Channa, why are you not posting the codes where the NPE occurs? If "@FML private Label myLabel" is null then your controller along with its FXML file is misconfigured. Posting the controller class and FXML file will be helpful. – Uluk Biy Jul 04 '14 at 04:03
  • Dear Biy, As you mention I have updated the question. Thanks in advance. – Channa Jul 07 '14 at 17:11