0

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.

jewelsea
  • 150,031
  • 14
  • 366
  • 406
T0rion
  • 13
  • 2
  • 2
    paste code , no pics , please – Giovanni Contreras May 01 '22 at 19:38
  • @jewelsea even with scenes it isn't working, the context menu says "rename reference" – T0rion May 02 '22 at 09:17
  • 1
    Fix the question. Provide a [mcve] as text formatted as code, both Java and CSS, so that your error can be reproduced. Compile the code, and if it compiles run it. If there are compile or runtime errors, post the full errors in the question, as text formatted as code. “It isn’t working” is not helpful in diagnosing the issue. “Rename reference” is just a refactoring option to rename something, it has nothing to do with your issue. – jewelsea May 02 '22 at 10:25
  • @jewelsea I updated my question, so it hopefully should be clear what my problem is about. Im new to this page, so sorry for the humbling start^^ – T0rion May 02 '22 at 18:51
  • Thanks for the update, I made a couple of minor edits (see [edit history](https://stackoverflow.com/posts/72079795/revisions)). To format blocks as code, just indent the block four spaces, with a blank line before and after the block. The code block editing icon in the StackOverflow editor will do this for you if you select a block of code and use it. – jewelsea May 02 '22 at 19:14
  • Thanks, the .root fixed the problem^^ Im not that experiencet in Javafx so thanks for your patience with me xD – T0rion May 02 '22 at 19:52

1 Answers1

1

Correctly find the CSS resource

Summarizing issues from the original post:

  1. Don’t create a new stage, use the stage passed to the start method.

  2. Style sheets are attributes of scenes or nodes, not stages. So get the stylesheets from the appropriate type.

  3. “Rename reference” is just a refactoring option to rename something, it has nothing to do with your issue.

  4. One issue is the resource is not found. To fix it, see (likely duplicate):

Answers to additional points

These were brought up in comments and question edits.

IntelliJ do also not colorcode the css code

Idea does do this (for both HTML and JavaFX CSS attribute names), but it might only be a feature of the paid (Ultimate edition). See:

strangely yesterday the IDE underlined getStyleSheets() and today it accepted it

That is because in your original code screenshot you were trying to call this method on a Stage and not a Scene. In your edit, you have fixed this and call the method on a Scene.

Also in the edit you use the correct name getStylesheets(), not getStyleSheets().

I then refactored it to the resources next to view.xml

You have no view.xml and .xml is not the right type for a .fxml file even if you were trying to use one.

Use the correct CSS selectors

Once the issues outlined above have been fixed, then the CSS stylesheet can be found, but it still needs to be valid and use the correct selectors to have it apply styles to nodes.

.Scene doesn't select anything, there is no style class called Scene.

Perhaps you mean .root (will set attributes globally for the scene).

Or maybe you want to set an ID on a container in the code and set the background just for that container. For example, in Java write:

layout1.setId("layout"); 

and in CSS write:

#layout { /** CSS styles **/ }

The asker notes in comments:

the .root fixed the problem

jewelsea
  • 150,031
  • 14
  • 366
  • 406