Post render event in JavaFX

2019-01-27 07:38发布

问题:

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.

回答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.



标签: javafx-2