问题
Language: JavaFX
IDE: Netbeans
Problem: I'm trying to add a css file to the stylesheet, but the first line of the following code always generates a NullPointerException:
String css = this.getClass().getResource("double_slider.css").toExternalForm();
scene.getStylesheets().add(css);
I've tried replacing "double_slider.css" with the full path. double_slider.css is currently located in the same package as the class that makes this call. I've also tried all of the variations found at http://introjava.wordpress.com/2012/03/21/linking-a-css-style-sheet-to-javafx-scene-graph/, with no success. Clean and build doesn't help either.
If I place the css file in the build folder where the .class files are dumped, the NullPointerException goes away. But then the css file does not work properly because it references other files in my project.
回答1:
put your yourname.css file directly under src directory.
scene.getStylesheets().add("yourname.css")
clean and build required
回答2:
I think your're missing the slashes which causes that the CSS file can't be found. Try to correct your path reference.
For example:
-root
--app/Main.java
--assets/double_slider.css
would be:
String css = this.getClass().getResource("/assets/double_slider.css").toExternalForm();
回答3:
I had the same problem. I use NetBeans 7.3 and JavaFX 2.2.7, JDK 7.0_21 on Win7.
My solution was to place the .css in the SAME folder as my Java file containing void start(Stage stage). So the Project view looks like this:
- ProjectName
- Source Packages
- pkgWhatever
- Main.java
- MyCssFile.css
- pkgWhatever
- Source Packages
(So the CSS file is IN the package, which I find really weird and contraintuitive. Some doc told me to put it in the root of the project so it could be found at runtime, but that didn't work for me in NB. My app now runs regardless of whether I start the file containing "start(..)" by hitting Ctrl+U or clicking Run on the project's context menu. And it doesn't matter whether I let NB put everything into a JAR or not.)
Here's the code that loads the CSS in the above situation:
URL url = this.getClass().getResource("controlStyle1.css");
if (url == null) {
System.out.println("Resource not found. Aborting.");
System.exit(-1);
}
String css = url.toExternalForm();
scene.getStylesheets().add(css);
Whereas this didn't work:
scene.getStylesheets().add("controlStyle1.css");
Hope this helps.
回答4:
I had the same problem (in NetBeans 8). I found a solution here : https://blog.idrsolutions.com/2014/04/use-external-css-files-javafx/
My resource file spreadsheet.css was here :
MyApp
-resources
--spreadsheet.css
-source packages
--fr.ccc.myapp.view
---mainView.java
---FXMLMain.fxml
In mainView.java :
File f = new File("resources/spreadsheet.css");
spreadsheet.getStylesheets().add("file:///" + f.getAbsolutePath().replace("\\", "/"));
Hope this helps.
回答5:
You can add your style.css directly in your .fxml file as an attribute to your root element as this stylesheets="@your_relative_path/style.css".
You can use @../style.css if you want to access the css file that is in your src folder
回答6:
Considering your old code:
String css =this.getClass().getResource("double_slider.css").toExternalForm();
scene.getStylesheets().add(css);
Try changing it to this new code and it will work:
screen.getStylesheets().add(getClass().getResource("double_slider.css").toExternalForm());
When you use getClass(), you don't need to use this keyword.
I hope this work for you. :)
回答7:
I made a small login example and this is how i link my styleshet.css
@Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("LoginUI.fxml"));
Scene scene = new Scene(root);
scene.getStylesheets().add(JavaFXLogin.class.getResource("stylesheet.css").toExternalForm());
stage.setScene(scene);
stage.show();
}
You can use this code line to link your css file. but the css file should be in your package.
scene.getStylesheets().add(JavaFXLogin.class.getResource("stylesheet.css").toExternalForm());
回答8:
Hmmm, are you on Netbeans? Try to "Clean and Build" the project.
回答9:
Have you initialized the scene object prior to setting style sheet?
scene = new Scene(myRootLayout, 600, 600); // for example
回答10:
It's pretty much simple.
this.scene.setUserAgentStylesheet(/resources/blabla.css);
This is what worked for me-
回答11:
in your file .java use this
Application.setUserAgentStylesheet(getClass().getResource("application.css")
.toExternalForm());
回答12:
Folder Structure
In the MedicalLibraryFx.java
scene.getStylesheets().add(getClass().getResource("/css/style.css").toString());
Folder structure when css is in the same directory as controller
scene.getStylesheets().add(getClass().getResource("style.css").toString());
回答13:
Assuming the file structure is something like this:
-root
--src
---resources
----double_slider.css
---package
----JavaFXFile.java
This is what worked for me:
scene.getStylesheets().add((new File("src/resources/double_slider.css")).toURI().toString());
回答14:
scene.getStylesheets().add("file:///home/fullpathname/file.css");
or
scene.getStylesheets().add("file:/home/fullpathname/file.css");
but after:
Run / Clean and Build Project
worked for me
NetBeans IDE 8.2 ; Java: 1.8.0_201 ;Linux 16.04
回答15:
All of the answers are missing one very important part and that's '/' before the name of the css file:
Folder structure:
src
resources
stylesheet.css
Load it like this, notice the starting slash before css file:
scene.getStylesheets().add(getClass().getResource("/stylesheet.css").toExternalForm())
回答16:
Try putting '@' the name of the file. It worked for me.
ex: '@main.css'
回答17:
.root {
/* background color for selected checkbox */
-fx-background-color:white;
}
.check-box:selected > .box {
/* background color for selected checkbox */
-fx-background-color: lime;
}
.check-box > .box {
/* background color of unselected checkbox */
-fx-background-color:grey;
}
.check-box:selected > .box > .mark,
.check-box:indeterminate > .box > .mark {
/* modify mark color */
-fx-background-color: blue;
}
来源:https://stackoverflow.com/questions/13946372/adding-css-file-to-stylesheets-in-javafx