diff --git a/modules/graphics/src/main/java/javafx/scene/Node.java b/modules/graphics/src/main/java/javafx/scene/Node.java --- a/modules/graphics/src/main/java/javafx/scene/Node.java +++ b/modules/graphics/src/main/java/javafx/scene/Node.java @@ -832,13 +832,15 @@ } } - private void invalidatedScenes(Scene oldScene, SubScene oldSubScene) { + // reapplyCSS should be true for root elements when they are added, and is false for children + // of the root element. This prevents CSS being reapplied recursively, as noted in JDK-8151756. + private void invalidatedScenes(Scene oldScene, SubScene oldSubScene, boolean reapplyCSS) { Scene newScene = sceneProperty().get(); boolean sceneChanged = oldScene != newScene; SubScene newSubScene = subScene; if (getClip() != null) { - getClip().setScenes(newScene, newSubScene); + getClip().setScenes(newScene, newSubScene, reapplyCSS); } if (sceneChanged) { updateCanReceiveFocus(); @@ -851,7 +853,7 @@ focusSetDirty(newScene); } scenesChanged(newScene, newSubScene, oldScene, oldSubScene); - if (sceneChanged) impl_reapplyCSS(); + if (sceneChanged && reapplyCSS) impl_reapplyCSS(); if (sceneChanged && !impl_isDirtyEmpty()) { //Note: no need to remove from scene's dirty list @@ -910,16 +912,16 @@ } } - final void setScenes(Scene newScene, SubScene newSubScene) { + final void setScenes(Scene newScene, SubScene newSubScene, boolean reapplyCSS) { Scene oldScene = sceneProperty().get(); if (newScene != oldScene || newSubScene != subScene) { scene.set(newScene); SubScene oldSubScene = subScene; subScene = newSubScene; - invalidatedScenes(oldScene, oldSubScene); + invalidatedScenes(oldScene, oldSubScene, reapplyCSS); if (this instanceof SubScene) { // TODO: find better solution SubScene thisSubScene = (SubScene)this; - thisSubScene.getRoot().setScenes(newScene, thisSubScene); + thisSubScene.getRoot().setScenes(newScene, thisSubScene, reapplyCSS); } } } @@ -6628,13 +6630,13 @@ } else { if (oldClip != null) { oldClip.clipParent = null; - oldClip.setScenes(null, null); + oldClip.setScenes(null, null, /* reapplyCSS */ false); oldClip.updateTreeVisible(false); } if (newClip != null) { newClip.clipParent = Node.this; - newClip.setScenes(getScene(), getSubScene()); + newClip.setScenes(getScene(), getSubScene(), /* reapplyCSS */ false); newClip.updateTreeVisible(true); } diff --git a/modules/graphics/src/main/java/javafx/scene/Parent.java b/modules/graphics/src/main/java/javafx/scene/Parent.java --- a/modules/graphics/src/main/java/javafx/scene/Parent.java +++ b/modules/graphics/src/main/java/javafx/scene/Parent.java @@ -266,7 +266,7 @@ relayout = true; } node.setParent(Parent.this); - node.setScenes(getScene(), getSubScene()); + node.setScenes(getScene(), getSubScene(), /* reapplyCSS*/ true); // assert !node.boundsChanged; if (node.isVisible()) { geomChanged = true; @@ -495,7 +495,7 @@ } if (old.getParent() == Parent.this) { old.setParent(null); - old.setScenes(null, null); + old.setScenes(null, null, /* reapplyCSS*/ false); } // Do not add node with null scene to the removed list. // It will not be processed in the list and its memory @@ -664,7 +664,7 @@ } for (int i=0; i