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 Nu
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:
(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.