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

CSS not reloading properly

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P4 P4
    • None
    • 8
    • javafx
    • Windows 7

      When trying to load CSS from a file, change that file & reload, the changes don't get picked up.

      Here is the test class:
      import javafx.application.Application;
      import javafx.event.ActionEvent;
      import javafx.event.EventHandler;
      import javafx.scene.Scene;
      import javafx.scene.control.Button;
      import javafx.scene.layout.StackPane;
      import javafx.stage.Stage;

      public class ReloadCss extends Application
      {
        String cssPath = ReloadCss.class.getResource("ReloadCss.css").toString();
        Scene scene;

        @Override
        public void start(Stage primaryStage) {
          Button btn = new Button("Reload CSS");
          btn.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
              scene.getStylesheets().clear();
              scene.getStylesheets().add(cssPath);
              System.out.println("Reloaded");
            }
          });

          StackPane root = new StackPane();
          root.getChildren().add(btn);

          scene = new Scene(root, 300, 250);
          scene.getStylesheets().add(cssPath);

          primaryStage.setTitle("Reload CSS Test");
          primaryStage.setScene(scene);
          primaryStage.show();
        }

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

      And a .css file:
      .root {
          -fx-font-size: 15px;
      }

      If the font size is changed between runs, it takes.
      If the font size is changed & the button clicked, it doesn't take.
      If scene.getStylesheets().add() adds a NEW stylesheet, that will work.

            kcr Kevin Rushforth
            ngalarneajfx Neil Galarneau (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported: