I would like to add a CSS file which is located somewhere on the filesystem. The purpose is to write an application where the user can add JavaFX CSS files (which are create
Your problem is that you aren't using a URL. Here you can find more documentation on how the CSS is loaded alongside the CSS reference.
If you have the URL as a String
you could set the CSS dynamically with an external file like so:
private boolean isANext = true;
public void start(Stage primaryStage) throws Exception {
Button button = new Button("Change CSS");
VBox vbox = new VBox(10);
vbox.setAlignment(Pos.CENTER);
vbox.getChildren().add(button);
scene = new Scene(vbox, 200, 200);
button.setOnAction(ev -> {
// Alternate two stylesheets just for this demo.
String css = isANext ? "file:///C:/temp/a.css" : "file:///C:/temp/b.css";
isANext = !isANext;
System.out.println("Loading CSS at URL " + css);
scene.getStylesheets().clear();
scene.getStylesheets().add(css);
});
primaryStage.setTitle("Title");
primaryStage.setScene(scene);
primaryStage.show();
}
In the a.css
.button {
-fx-text-fill: white;
-fx-background-color: red;
}
And in b.css
.button {
-fx-text-fill: white;
-fx-background-color: black;
}