Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8185709

CSS class not removed when node not parented

XMLWordPrintable

    • x86_64
    • windows_7

      FULL PRODUCT VERSION :
      java version "1.8.0_144"
      Java(TM) SE Runtime Environment (build 1.8.0_144-b01)
      Java HotSpot(TM) 64-Bit Server VM (build 25.144-b01, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      Windows 7, 64 bit

      A DESCRIPTION OF THE PROBLEM :
      I appeared to have discovered a case in JavaFX where a TextField is rendered using the wrong style class.

      The textfield retains its styling using the style class that has been removed. This only appears to happen in the following situation

       * style class removed whilst Node not attached to Scene AND
       * parent `GridPane` has a style-class assigned, even if that class doesn't have content.

      Also posted here :https://stackoverflow.com/questions/45440102/javafx-css-class-not-removed-when-node-disconnected

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Run the example included in the report

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      TextField should have red background removed
      ACTUAL -
      TextField retains red background

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      public class RenderBug extends Application {

          private static final String ERROR = "error";

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

          @Override
          public void start(Stage primaryStage) throws Exception {
              TextField field = new TextField();
              GridPane grid = new GridPane();
              grid.getStyleClass().add("some-random-class");
              grid.add(field, 0, 0);

              StackPane stack = new StackPane(grid);

              Scene scene = new Scene(stack);
              scene.getStylesheets().add("/foo.css");
              primaryStage.setWidth(300);
              primaryStage.setHeight(300);
              primaryStage.setScene(scene);
              primaryStage.show();

              Timeline line = new Timeline();

              line.getKeyFrames().add(new KeyFrame(Duration.seconds(4), event -> {
                  field.getStyleClass().add(ERROR);
              }));
              line.getKeyFrames().add(new KeyFrame(Duration.seconds(5), event -> {
                  stack.getChildren().remove(grid);
              }));
              line.getKeyFrames().add(new KeyFrame(Duration.seconds(6), event -> {
                  field.getStyleClass().remove(ERROR);
              }));
              line.getKeyFrames().add(new KeyFrame(Duration.seconds(7), event -> {
                  stack.getChildren().add(grid);
                  System.out.println(field.getStyleClass());
              }));

              line.play();
          }

      }


      CSS file foo.css

      .text-field.error {
          -fx-background-color: red;
      }

      .some-random-class {
          /** no content required */
      }


      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Make sure node is attached to scene before making updates to styleClass

        1. foo.css
          0.1 kB
        2. RenderBug.java
          2 kB

            aghaisas Ajit Ghaisas
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: