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.
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.
- duplicates
-
JDK-8094828 Reload of an updated css file has no effect
-
- Resolved
-