adding css file to stylesheets in javafx

前端 未结 18 1182
[愿得一人]
[愿得一人] 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:50

    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. :)

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-06 05:53
    .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;
     }
    
    0 讨论(0)
  • 2020-12-06 05:57

    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

    0 讨论(0)
  • 2020-12-06 05:58

    Hmmm, are you on Netbeans? Try to "Clean and Build" the project.

    0 讨论(0)
  • 2020-12-06 05:59

    It's pretty much simple.

    this.scene.setUserAgentStylesheet(/resources/blabla.css);
    

    This is what worked for me-

    0 讨论(0)
提交回复
热议问题