I have created a new JavaFx project in IntelliJ and the main JavaFx Application is:
HelloApplication.java
package com.example.demo1;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.stage.Stage;
import java.io.IOException;
public class HelloApplication extends Application {
@Override public void start(Stage stage) throws IOException {
FXMLLoader fxmlLoader = new FXMLLoader(HelloApplication.class.getResource("hello-view.fxml"));
Scene scene = new Scene(fxmlLoader.load(), 320, 240);
scene.getStylesheets().clear();
scene.getStylesheets().addAll();
// SOLVED: this was the solution. This came here after I have posted the question
// scene.getRoot().getStylesheets().clear();
stage.setTitle("Hello!");
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch();
}
}
HelloController.java
package com.example.demo1;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
public class HelloController {
@FXML private Label welcomeText;
@FXML protected void onHelloButtonClick() {
welcomeText.setText("Welcome to JavaFX Application!");
}
}
hello-view.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.control.Button?>
<VBox alignment="CENTER" spacing="20.0" xmlns:fx="http://javafx.com/fxml" stylesheets="@darktheme.css"
fx:controller="com.example.demo1.HelloController">
<padding>
<Insets bottom="20.0" left="20.0" right="20.0" top="20.0"/>
</padding>
<Label fx:id="welcomeText"/>
<Button text="Hello!" onAction="#onHelloButtonClick"/>
</VBox>
darktheme.css
.root {
-fx-accent: #1e74c6;
-fx-focus-color: -fx-accent;
-fx-base: #373e43;
-fx-control-inner-background: derive(-fx-base, 35%);
-fx-control-inner-background-alt: -fx-control-inner-background ;
}
Where I have set the style with:
stylesheets="@darktheme.css"
And I am trying to create a toggle with the CheckBox. The .CSS works. I have everything dark. However if I understood everything correctly, the lines in code:
scene.getStylesheets().clear(); // Clear any stylesheets that were added to the scene
scene.getStylesheets().addAll(); // Add an empty list of stylesheets to reset to the default style
Should have reverted the Scene and Stage to the original JavaFx theme. However when I run the code, everything is dark. Could anyone explain this to me?