diff --git a/modules/javafx.graphics/src/main/java/javafx/scene/Parent.java b/modules/javafx.graphics/src/main/java/javafx/scene/Parent.java index 6d287b5d670..725af3a6cde 100644 --- a/modules/javafx.graphics/src/main/java/javafx/scene/Parent.java +++ b/modules/javafx.graphics/src/main/java/javafx/scene/Parent.java @@ -991,10 +991,13 @@ boolean isPerformingLayout() { private double minHeightCache = -1; void setLayoutFlag(LayoutFlags flag) { + // Needs to be set before needsLayout is updated, as otherwise a listener that + // calls isNeedsLayout() might see the old value. + layoutFlag = flag; + if (needsLayout != null) { needsLayout.set(flag == LayoutFlags.NEEDS_LAYOUT); } - layoutFlag = flag; } private void markDirtyLayout(boolean local, boolean forceParentLayout) { diff --git a/modules/javafx.graphics/src/shims/java/javafx/scene/ParentShim.java b/modules/javafx.graphics/src/shims/java/javafx/scene/ParentShim.java index 813f7ffa52b..93318796e05 100644 --- a/modules/javafx.graphics/src/shims/java/javafx/scene/ParentShim.java +++ b/modules/javafx.graphics/src/shims/java/javafx/scene/ParentShim.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2015, 2022, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2015, 2025, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -45,6 +45,10 @@ public static List getManagedChildren(Parent p) { return p.getManagedChildren(); } + public static void setNeedsLayout(Parent p, boolean value) { + p.setNeedsLayout(value); + } + public static List test_getRemoved(Parent p) { return p.test_getRemoved(); } diff --git a/modules/javafx.graphics/src/test/java/test/javafx/scene/ParentTest.java b/modules/javafx.graphics/src/test/java/test/javafx/scene/ParentTest.java index 17f5958eb43..da966c612ff 100644 --- a/modules/javafx.graphics/src/test/java/test/javafx/scene/ParentTest.java +++ b/modules/javafx.graphics/src/test/java/test/javafx/scene/ParentTest.java @@ -53,6 +53,7 @@ import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertSame; import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.fail; import static org.junit.jupiter.api.Assertions.assertThrows; @@ -520,6 +521,23 @@ public void needsLayoutPropertyIsReadOnly() { () -> { var _ = (Property)new Group().needsLayoutProperty(); }); } + @Test + public void isNeedsLayoutReturnsCorrectValueInListener() { + var g = new Group(); + g.layout(); + assertFalse(g.isNeedsLayout()); + + boolean[] flags = new boolean[2]; + g.needsLayoutProperty().subscribe(value -> { + flags[0] = value; + flags[1] = g.isNeedsLayout(); + }); + + ParentShim.setNeedsLayout(g, true); + assertTrue(flags[0]); + assertTrue(flags[1]); + } + private static class LGroup extends Group { private boolean layoutCalled;