Node.lookupAll behaves differently before Scene is shown

Scott Palmer swpalmer at gmail.com
Fri Dec 23 22:49:37 UTC 2022


I just want to make sure this is not expected behaviour. I don’t think so, the documentation for lookupAll doesn’t mention anything related, but maybe I missed something somewhere else.

I was just coding something to query the Scene for all SplitPanes and save/restore the divider positions for when my application is exiting and launching and came across this issue.
My UI is fully constructed (at least in terms of all the SplitPanes in the scene graph) before it is shown.

This  code:

	window.getScene().getRoot().lookupAll(".split-pane")

returns a different number of Nodes if I call it before showing the window versus after showing the window.  Specifically, if I call it before showing the window it appears to only return the first SplitPane found in the scene graph, but calling it in an event handler for the window shown event I get all three.

This can be demonstrated with the following program:


import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.SplitPane;
import javafx.scene.control.TextArea;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
import javafx.stage.Window;

public class LookupAll extends Application {
    Window mainWindow;
    
    public static void main(String[] args) {
        launch(args);
    }
    
    @Override
    public void start(Stage primaryStage) throws Exception {
        var bp = new BorderPane(new SplitPane(new TextArea(),new SplitPane(new TextArea(),new SplitPane(new TextArea()))));
        var scene = new Scene(bp);
        primaryStage.setScene(scene);
        mainWindow = primaryStage;
        System.out.println("Before showing:");
        countSplitPanes();
        primaryStage.setOnShown(we -> {
            System.out.println("After showing:");
            countSplitPanes();
        });
        primaryStage.show();
    }
    
    private void countSplitPanes() {
        var splitPanes = mainWindow.getScene().getRoot().lookupAll(".split-pane");
        System.out.printf("Found %d SpitPanes: %s\n",splitPanes.size(), splitPanes);
    }
}



Before showing:
Found 1 SpitPanes: [SplitPane at 16bca2d9[styleClass=split-pane]]
After showing:
Found 3 SpitPanes: [SplitPane at 16bca2d9[styleClass=split-pane], SplitPane at 7078ef3c[styleClass=split-pane], SplitPane at 56a29626[styleClass=split-pane]]


Regards,

Scott

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/openjfx-dev/attachments/20221223/4cfd2efb/attachment.htm>


More information about the openjfx-dev mailing list