adding css file to stylesheets in javafx

前端 未结 18 1288
[愿得一人]
[愿得一人] 2020-12-06 05:27

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

18条回答
  •  一生所求
    2020-12-06 05:52

    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

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

提交回复
热议问题