JDK-8200280 : CSS not applied to children if parent removed and then re-added to scene
  • Type: Bug
  • Component: javafx
  • Sub-Component: base
  • Affected Version: 8u102,9
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows
  • CPU: x86_64
  • Submitted: 2018-03-26
  • Updated: 2018-03-27
  • Resolved: 2018-03-27
Related Reports
Duplicate :  
Description
FULL PRODUCT VERSION :
java version "9.0.4"
Java(TM) SE Runtime Environment (build 9.0.4+11)
Java HotSpot(TM) 64-Bit Server VM (build 9.0.4+11, mixed mode)

ADDITIONAL OS VERSION INFORMATION :
Windows 7

A DESCRIPTION OF THE PROBLEM :
CSS attributes including at least background-color, border-color and border-width are not applied to a child node if its parent is removed from the scene and then re-added.

When the node is added again then it appears without any of the styles set via "inline" styles i.e. Node.setStyle(). This is also the case when using style-classes and an external stylesheet. The test case attached uses "inline" styles to keep it minimal. 



STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
1) Run the test case
2) Click on the stage
3) Click on the stage
4) Click on the stage


EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
1) Run the test case
2) Click on the stage  <== expect overlay in red with black border, 5px
3) Click on the stage  <== expect overlay to be hidden
4) Click on the stage  <== expect overlay in red with black border, 5px, i.e. same as step 2

ACTUAL -
1) Run the test case
2) Click on the stage  <== expect overlay in red with black border, 5px
3) Click on the stage  <== expect overlay to be hidden
4) Click on the stage  <== overlay shown without background or border

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class CssNotApplied extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        StackPane topLevel = new StackPane();
        Scene scene = new Scene(topLevel, 400, 300);

        StackPane overlay = new StackPane();
        overlay.setStyle("-fx-background-color: green");

        primaryStage.setScene(scene);
        topLevel.setOnMouseClicked(event -> {
            HBox box = new HBox(new Label("hello rigby"));
            box.setStyle("-fx-background-color: red;-fx-border-color:black;-fx-border-width:5px;");
            overlay.getChildren().setAll(box);
            
            // toggle overlay
            if (!topLevel.getChildren().contains(overlay)) {
                topLevel.getChildren().add(overlay);
            } else {
                topLevel.getChildren().remove(overlay);
            }
        });
        primaryStage.show();
    }
}
---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
Add content to overlay AFTER it has been added to topLevel


Comments
This is a duplicate of JDK-8185709, which has been fixed in JDK 10.
27-03-2018