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
}