2

I'm trying to add a click event listener to the label of all column-headers of a TableView, as follows:

for (final Node header : tblView.lookupAll(".column-header > .label")) {         
    if ((header != null) && (header instanceof Label)) {
        final Label headerLabel = (Label) header;
        // ...
    }
}

Now, the problem is that if I do this in the initialize()-function of the Controller, the Scenegraph is not yet rendered and the above code won't work. Hence my question: Is there some kind of a post-render event?

many thanks in advance.

Uluk Biy
  • 48,655
  • 13
  • 146
  • 153
dsd
  • 551
  • 5
  • 17

1 Answers1

1

There is a WINDOW_SHOWN event in javafx.stage.WindowEvents. That is not (imo) "Post render event" but you can utilize it in similar manner, by adding an event handler to the Stage (which extends from Window).

In the initialize method of controller class, get the primary stage and then:

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

Hope this helps, since didn't try myself.

Uluk Biy
  • 48,655
  • 13
  • 146
  • 153
  • This event handler works as expected, thanks! However, getting my hands on the primary stage within `initialize()` of the Main Controller is a problem on its own, as it seems... One solution is to grab the controller in the Main Class, but that couples the Application class and the Main Controller. – dsd Jun 05 '13 at 06:57
  • @Biy My application have number of Controllers and I need to make this while middle of some UI flow. When I add the WindowEvent.WINDOW_SHOWN event handler as you mention, it will try to execute this event handler place which I have declared that mean with the execution of Application's start(Stage method), then I got some Null Pointer Exceptions. Can you please tell me how I can fix this issue. Thanks. – Channa Jul 03 '14 at 11:07
  • @Channa, answered to your related question. Please have a look. – Uluk Biy Jul 03 '14 at 13:56