Your problem is that you are not using the URL. Here you can find additional documentation on how CSS loads with a link to CSS.
If you have a URL like String , you can dynamically set CSS with an external file as follows:
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 -> {
In a.css
.button { -fx-text-fill: white; -fx-background-color: red; }
And in b.css
.button { -fx-text-fill: white; -fx-background-color: black; }
source share