Post render event in JavaFX

2019-01-27 07:53发布

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.

标签: javafx-2
1条回答
对你真心纯属浪费
2楼-- · 2019-01-27 08:29

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.

查看更多
登录 后发表回答