2

I need to perform some checks/testing only when a node's layout operation is done completely. When I am working with JavaFX 8 version, I use to rely on the needsLayout property of Parent node. This property gets updated after all the layout computing of the Parent node and its children nodes.

Below is the code of Parent's layout() in JavaFX 8:

public final void layout() {
    switch(layoutFlag) {
        case CLEAN:
            break;
        case NEEDS_LAYOUT:
            if (performingLayout) {
                break;
            }
            performingLayout = true;
            layoutChildren();
            // Intended fall-through
        case DIRTY_BRANCH:
            for (int i = 0, max = children.size(); i < max; i++) {
                final Node child = children.get(i);
                if (child instanceof Parent) {
                    ((Parent)child).layout();
                } else if (child instanceof SubScene) {
                    ((SubScene)child).layoutPass();
                }
            }
            // Flag is updated only after all the children's layout is done
            setLayoutFlag(LayoutFlags.CLEAN); 
            performingLayout = false;
            break;
    }
}

But after switching to JavaFX 18, looks like this does'nt work anymore. Because the layout flag is changed way ahead even before the Parent calls its layoutChildren.

Below is the code of Parent's layout() in JavaFX 18:

public final void layout() {
    // layoutFlag can be accessed or changed during layout processing.
    // Hence we need to cache and reset it before performing layout.
    LayoutFlags flag = layoutFlag;
    setLayoutFlag(LayoutFlags.CLEAN);
    switch(flag) {
        case CLEAN:
            break;
        case NEEDS_LAYOUT:
            if (performingLayout) {
                break;
            }
            performingLayout = true;
            layoutChildren();
            // Intended fall-through
        case DIRTY_BRANCH:
            for (int i = 0, max = children.size(); i < max; i++) {
                final Node child = children.get(i);
                currentLayoutChild = child;
                if (child instanceof Parent) {
                    ((Parent)child).layout();
                } else if (child instanceof SubScene) {
                    ((SubScene)child).layoutPass();
                }
            }
            currentLayoutChild = null;
            performingLayout = false;
            break;
    }
}

Considering the above changes in the new version., is there any way I can know if the layout of a Parent node is completed?

Note: I am aware of the Scene's postLayoutPulseListener. But that does call after the complete layoutPass. Here I am trying to look on individual Parent node level.

Sai Dandem
  • 8,229
  • 11
  • 26
  • 1
    It’s not really clear, to me, why you would need this. If you’re using predefined layout panes, there’s no real need to test that (testing of those is the responsibility of the authors of the library). If you have a custom layout, the supported mechanism for that is to override `layoutChildren()`, and you can implement your test there. – James_D Aug 08 '22 at 01:52
  • @James_D I understand that this is a bit typical or tricky question. I will try to update the question with some minimal example for my scenario. – Sai Dandem Aug 08 '22 at 03:26
  • There's [JDK-8176902](https://bugs.openjdk.org/browse/JDK-8176902), which requests some sort of `Robot#waitForIdle()` method be added. That seems a possible solution, though is obviously not helpful at the moment. Not sure of an immediate solution, but I wonder if the change in behavior that you noted could be considered a bug? – Slaw Aug 08 '22 at 05:54
  • @Slaw, I am not sure whether it is a bug or not (as it is intentionally changed with some comments). The only thing that bothers me is about all the existing listeners(in FX8) that are binded with needsLayoutProperty are now notified with a different behaviour. And did they considered this when changing the behavior? – Sai Dandem Aug 08 '22 at 06:01
  • the change happened quite a while ago (fx9): https://bugs.openjdk.org/browse/JDK-8137252 - it feels a bit counter-intuitive to reset the layout flag before the layout is complete, but was accepted after extensive discusssion (didn't dig into the arguments :) – kleopatra Aug 08 '22 at 09:21

0 Answers0