0

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?

Jack
  • 129
  • 9
  • 1
    [mcve] please .. – kleopatra Apr 24 '23 at 12:55
  • 1
    *"the lines in code ... Should have reverted the Scene and Stage to the original JavaFx theme"*. Why? You added the `darktheme.css` stylesheet to the `VBox`, not to the `Scene`. – James_D Apr 24 '23 at 12:57
  • @James_D How do I set the stylesheet to the Scene? I have set the Stylesheet to Root node. I thought that was the highest thing I could set the style to. A good suggestion though. I will try to tinker with it a bit. Thank you. – Jack Apr 24 '23 at 13:12
  • @kleopatra Ok. Thanks. I have just created a new project and added a few of those lines. I will try to rewrite the question. Now I have already messed everything up again but I can start anew and focus just on my things. – Jack Apr 24 '23 at 13:13
  • @James_D Thank you!! :) adding: scene.getRoot().getStylesheets().clear(); did the trick :) – Jack Apr 24 '23 at 13:15

1 Answers1

1

The answer was the commented code in the question. Which I added alter after @James_D pointed to me that changing sth. to the scene does not trickle down to root and lower.

After adding:

scene.getRoot().getStylesheets().clear();

My code worked in the way as I have hoped it would work. I thought that by removing stylesheets from the (Scene) scene everything would get deleted but it did not. Thank you all for your help.

Jack
  • 129
  • 9