1
Scene listenMenu = new Scene(root, 250, 272);
listenMenu.getStylesheets().add("styles.css");

This always worked for me to load my css file, but after a small IntelliJ update it gives me this error:

Juni 20, 2018 1:16:45 NACHM. com.sun.javafx.css.StyleManagerloadStylesheetUnPrivileged WARNING: Resource "styles.css" not found.

I tried to look for a solution on here but but nothing works. Taking the url and the toExternalForm() like this:

listenMenu.getStylesheets().add(getClass().getResource("src/main/resources/styles.css").toExternalForm());

throws this Exception:

    Exception in Application start method
Exception in thread "main" java.lang.reflect.InvocationTargetException
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:564)
    at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:941)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:973)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:198)
    at java.base/java.lang.Thread.run(Thread.java:844)
Caused by: java.lang.NullPointerException
    at HDMStuttgart.GUI.ListGUI.start(ListGUI.java:37)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:919)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$11(PlatformImpl.java:449)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$9(PlatformImpl.java:418)
    at java.base/java.security.AccessController.doPrivileged(Native Method)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:417)
    at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:175)
    ... 1 more

Project Stucture: - main -- java --- HDMStuttgart ---- GUI ----- ListGui.java <-- The java file

-- resources --- styles.css <-- the css file

Any suggestions would be highly appretiated!

Japhet
  • 161
  • 1
  • 2
  • 12
  • put the `CSS` file in the same folder as your `Java` file calling it. Then do `listenMenu.getStylesheets().add(getClass().getResource("styles.css").toExternalForm());` – SedJ601 Jun 20 '18 at 12:06
  • @Sedrick tried it, still doesn't work. It throws the same Exceptions as mentioned above – Japhet Jun 20 '18 at 12:11
  • Show your project structure. – SedJ601 Jun 20 '18 at 12:13
  • @Sedrick you want to see i what folders the files are right? Is there a good way to just copy paste that in here? – Japhet Jun 20 '18 at 12:26
  • 1
    Possible duplicate of [adding css file to stylesheets in javafx](https://stackoverflow.com/questions/13946372/adding-css-file-to-stylesheets-in-javafx) – Blake Ordway Jun 20 '18 at 13:41

2 Answers2

3

try this: create a Java file

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 HelloStyledWorld extends Application {

    @Override
    public void start(Stage primaryStage) {
        Button btn = new Button();
        btn.setText("Say 'Hello Styled World'");
        btn.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent event) {
                System.out.println("Hello Styled World!");
            }
        });

        StackPane root = new StackPane();
        root.getChildren().add(btn);

        Scene scene = new Scene(root, 300, 250);
        scene.getStylesheets().add(getClass().getResource("style.css").toExternalForm());

        primaryStage.setTitle("Hello Styled World!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

put the style.class in the same package as the .java file (or in the corresponding resource folder if you are using one.

verify that the build includes the correct css file into output folder. you can

System.out.println(getClass().getResource("style.css").toExternalForm());

to console to see where JavaFX is searching for the css and check if the css is available there.

lumo
  • 790
  • 1
  • 8
  • 23
  • It throws the same Exeption as I wrote above. I copied your code in a test class and changed the css name to match my file. There seems to be something wrong with the application class, cause it throws the Exception before it can go into that test method – Japhet Jun 21 '18 at 08:40
  • verify that your css file exists in the location the application looks for. if so do not refer to your existing css file - create an EMPTY style.css - probably you have some syntax errors in your css. – lumo Jun 21 '18 at 08:50
  • how do I verify that? if I copy the System.out.print... from above it still throws the Exception. – Japhet Jun 21 '18 at 09:00
  • however I just created a new css file in the same folder as the old one, copied everything from the old file to the new one and did "scene.getStylesheets().add("style.css");". Now everything works.. this doesnt make any sense – Japhet Jun 21 '18 at 09:09
  • 1
    probably you edited the file in a different editor and had different file encoding or some control chars, which do not get shown in the editor (and get dropped by copy&paste) – lumo Jun 21 '18 at 09:19
  • it is helpfull to invoke `System.getProperty("user.dir");` to get the path from which your application is running (f.e. intellij builds to someplace in users/Idea/...) , then you can just build the complete path by adding path + "\\stylesheet.css" .... this way, you can make sure the path is still correct when you deploy the application, or at least debug it – clockw0rk May 06 '22 at 14:23
1

If you are using scene.getStylesheets().add("com/whatever/stylesheet.css"); You have to link the whole path where it is located including the package after the src folder, not the src folder itself though.

If you are using scene.getStylesheets().add(getClass().getResource("stylesheet.css").toExternalForm());

The css file has to be located in the same dir as the java file you are loading it from. You only have to provide the file name.

If you are wanting to make it in a resource folder in the src folder then do that. If not I don't know how.