1

I am working through the official JavaFX tutorial. I am trying to add the code from Example 5-1 as follows to my scene, which is part of the "Main" class as per the instructions:

Scene scene = new Scene(grid, 300, 275);
primaryStage.setScene(scene);
scene.getStylesheets().add(Login.class.getResource("Login.css").toExternalForm());
primaryStage.show();

I'm using JavaFX runtime version 8.0.72-b15 and IntelliJ IDEA 15.0.5. I have stored my "Login.css" file in the folder:

C:\Users\XXXXXXXX\IdeaProjects\Login\src

and my project files are structured as follows:

  • Login
    • .idea
    • out
    • src
      • sample
        • Controller.java
        • Main.java
        • sample.fxml
      • background.jpg
      • Login.css

When I run "Main" I get the following error message:

Information:Using javac 1.8.0_77 to compile java sources
Information:java: Errors occurred while compiling module 'Login'
Information:6/9/2016 2:04 PM - Compilation completed with 1 error and 0 warnings in 571ms
C:\Users\XXXXXXXX\IdeaProjects\Login\src\sample\Main.java
Error:(69, 36) java: cannot find symbol
  symbol:   class Login
  location: class sample.Main

This answer seems to imply that the custom CSS file should be under the "src" folder, but it doesn't appear to be working for me. How can I get my JavaFX project to import this custom CSS file?

4
  • try putting the css in a new srcFolder named 'resources' in it create a package named 'css' and put into it the Login.css.Then call it like this ("/css/Login.css). Commented Jun 9, 2016 at 18:20
  • GoXrPlus, thank you very much for your suggestion. Based on @jewelsea 's suggestion, I modified that line of my code into the following: 'scene.getStylesheets().add(sample.Main.class.getResource("/Login.css").toExternalForm());' Changing the "Login" class call to a "sample.Main" class call and also the adding of the "/" symbol before "Login.css" made it work. Thanks for your help! Commented Jun 9, 2016 at 19:46
  • M Lev to sent a message to other you should use @username.Just for informing :) Commented Jun 10, 2016 at 9:31
  • GoXr3Plus, thanks :) I think if you made the first comment, it doesn't want me to use the @ for your username though, I am getting an error message with it. It's all good though :) Commented Jun 10, 2016 at 17:06

3 Answers 3

3

You don't have a Login class... you only have a sample.Main class, so the Login.class reference in your code won't resolve (that error is unrelated to CSS).

Once you fix the class reference error, you also need to fix the reference to the css file location (as your Login.css file is not in the sample package, but is instead in the source root, so it will eventually be copied to the root of your classpath). To reference a resource in the root of the classpath, prepend / to the resource name, i.e. /Login.css.

So, after these changes, you have, the following code (which you verified works in previous comments):

scene.getStylesheets().add(sample.Main.class.getResource("/Login.css").toExtern‌​alForm());
Sign up to request clarification or add additional context in comments.

Comments

0
fxViewObject.getStylesheets().add(this.class.getResource("/Login.css").toExternalForm());

1 Comment

While this might be an answer to the question, some more background information would help to understand your solution.
0
scene.getStylesheets().add(this.getClass().getResource("/stylesheet.css").toString());

Recommend typing this.getclass() you can copy paste it to any class and any package. Also toString() works too, it will call toExternalForm() anyway. Maybe it is even better because any java programmer knows about toString. The easier to understand the code is the better.

(maybe there is a minor performance difference, someone who knows the in and outs of java can comment on this btw. On bytecode level it won't swap out I checked that, only in runtime.)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.