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

Looked-up color fails for -fx-background-color in JavaFX CSS file

XMLWordPrintable

    • x86_64
    • os_x

      A DESCRIPTION OF THE PROBLEM :
      If I change the stylesheet for a button, and the button's -fx-background-color references a looked-up color, JavaFX writes a message like this to the console.

      Jun 08, 2021 2:38:28 PM javafx.scene.CssStyleHelper calculateValue
      WARNING: Caught 'java.lang.ClassCastException: class java.lang.String cannot be cast to class javafx.scene.paint.Paint (java.lang.String is in module java.base of loader 'bootstrap'; javafx.scene.paint.Paint is in module javafx.graphics@11.0.11 of loader 'platform')' while converting value for '-fx-background-color' from rule '*.button-primary' in stylesheet file:/Users/will/github/CssTest/out/production/CssTest/csstest/dark.css


      REGRESSION : Last worked in version 8u291

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      See the Java code and CSS files included below. Run the program. It displays a button labeled "CSS File". Press the button. The button will turn red, and the message will appear on the console.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Not to get an alarming message on the console.
      ACTUAL -
      Here is the message.

      Jun 08, 2021 2:38:28 PM javafx.scene.CssStyleHelper calculateValue
      WARNING: Caught 'java.lang.ClassCastException: class java.lang.String cannot be cast to class javafx.scene.paint.Paint (java.lang.String is in module java.base of loader 'bootstrap'; javafx.scene.paint.Paint is in module javafx.graphics@11.0.11 of loader 'platform')' while converting value for '-fx-background-color' from rule '*.button-primary' in stylesheet file:/Users/will/github/CssTest/out/production/CssTest/csstest/dark.css


      ---------- BEGIN SOURCE ----------
      File: CssTest.java

      /*
       * To change this license header, choose License Headers in Project Properties.
       * To change this template file, choose Tools | Templates
       * and open the template in the editor.
       */
      package csstest;

      import javafx.application.Application;
      import javafx.scene.Scene;
      import javafx.scene.control.Button;
      import javafx.scene.layout.StackPane;
      import javafx.stage.Stage;


      public class CssTest extends Application {
          static final String DARK =
              CssTest.class.getResource("dark.css").toExternalForm();
          static final String MEDIUM =
                  CssTest.class.getResource("medium.css").toExternalForm();

          StackPane root;

          @Override
          public void start(Stage primaryStage) {
              Button btn = new Button();
              btn.setText("Set CSS");
              btn.setOnAction(evt -> {
                  setTheme(MEDIUM);
              });
              btn.getStyleClass().clear();
              btn.getStyleClass().add("button-primary");
              
              root = new StackPane();
              root.getChildren().add(btn);
              root.getStyleClass().add("root");

              Scene scene = new Scene(root, 300, 250);
              
              primaryStage.setTitle("Hello World!");
              primaryStage.setScene(scene);
              primaryStage.show();

              setTheme(DARK);

          }

          private void setTheme(String theme) {
              System.out.println("setTheme: " + theme);
              // If I clear() the stylesheet, I get the messages.
              root.getStylesheets().clear();
              root.getStylesheets().add(theme);

              // If I add the new style sheet, and then remove any predecessors, I don't.
      // while (root.getStylesheets().size() > 1) {
      // root.getStylesheets().remove(0);
      // }
          }

          /**
           * @param args the command line arguments
           */
          public static void main(String[] args) {
              launch(args);
          }
      }

      Plus two CSS files.

      /* File: medium.css */
      .button-primary {
      -fx-background-color: -theme-button;
          -fx-font-family: helvetica;
      }
      .root {
      -theme-button: #FF0000;
      }

      /* File: dark.css */
      .button-primary {
      -fx-background-color: -theme-button;
          -fx-font-family: helvetica;
      }
      .root {
      -theme-button: #0679DE;
      }

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

      CUSTOMER SUBMITTED WORKAROUND :
      The problem doesn't occur if the java code adds the new stylesheet and then removes the old one instead of calling clear() on getStylesheets(); see the code.

      In addition, if I remove the -fx-font-family style from button-primary, the message does not appear.

      FREQUENCY : always


        1. CssTest.java
          2 kB
        2. dark.css
          0.1 kB
        3. medium.css
          0.1 kB

            Unassigned Unassigned
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated: