I'm trying to use css in combination with javaFX. To get a css-Stylesheet to a design I tried something like this:
public void start(Stage stage) throws IOException {
stage = window;
window.setTitle("cssTest");
button1 = new Button("click me");
button1.setOnAction(e -> System.out.println("Hello!"));
StackPane layout1 = new StackPane();
layout1.getChildren().add(button1);
scene1 = new Scene(layout1, 300, 250);
scene1.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
window.setScene(scene1);
window.show();
}
First the getStylesheets() part wasn't working at all my IDE (IntelliJ Idea Community version) underlined it and told me to rename the reference. I only wanted to test it and tried to turn the background of the application black. Here is my css Code (The file is named application.css):
.Scene{
-fx-background-color: black;
}
IntelliJ do also not colorcode the css code. I first created the css file within the package where my start and my main-Method are, but this throws errors. See here:
"C:\Program Files\Java\jdk-16.0.2\bin\java.exe" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2021.3.2\lib\idea_rt.jar=62160:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2021.3.2\bin" -Dfile.encoding=UTF-8 -classpath C:\Users\timla\.m2\repository\org\openjfx\javafx-controls\16\javafx-controls-16.jar;C:\Users\timla\.m2\repository\org\openjfx\javafx-graphics\16\javafx-graphics-16.jar;C:\Users\timla\.m2\repository\org\openjfx\javafx-base\16\javafx-base-16.jar;C:\Users\timla\.m2\repository\org\openjfx\javafx-fxml\16\javafx-fxml-16.jar -p C:\Users\timla\.m2\repository\org\openjfx\javafx-base\16\javafx-base-16-win.jar;C:\Users\timla\.m2\repository\org\openjfx\javafx-fxml\16\javafx-fxml-16-win.jar;C:\Users\timla\.m2\repository\org\controlsfx\controlsfx\11.1.0\controlsfx-11.1.0.jar;C:\Users\timla\.m2\repository\org\kordamp\bootstrapfx\bootstrapfx-core\0.4.0\bootstrapfx-core-0.4.0.jar;C:\Users\timla\IdeaProjects\fxDemo\target\classes;C:\Users\timla\.m2\repository\org\openjfx\javafx-graphics\16\javafx-graphics-16-win.jar;C:\Users\timla\.m2\repository\org\openjfx\javafx-controls\16\javafx-controls-16-win.jar;C:\Users\timla\.m2\repository\com\dlsc\formsfx\formsfx-core\11.3.2\formsfx-core-11.3.2.jar -m com.example.fxdemo/com.example.fxdemo.HelloApplication
Exception in Application start method
java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:78)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:567)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:464)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:363)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:78)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:567)
at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1071)
Caused by: java.lang.RuntimeException: Exception in Application start method
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:900)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:195)
at java.base/java.lang.Thread.run(Thread.java:831)
Caused by: java.lang.NullPointerException: Cannot invoke "java.net.URL.toExternalForm()" because the return value of "java.lang.Class.getResource(String)" is null
at com.example.fxdemo/com.example.fxdemo.HelloApplication.start(HelloApplication.java:32)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:846)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:474)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:447)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:446)
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:174)
... 1 more
Exception running application com.example.fxdemo.HelloApplication
Process finished with exit code 1
I then refactored it to the resources next to view.xml. Now the application starts but the Stylesheet is not applied to it. strangely yesterday the IDE underlined getStyleSheets() and today it accepted it, but it still isn't working.