adding css file to stylesheets in javafx

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

    in your file .java use this

    Application.setUserAgentStylesheet(getClass().getResource("application.css")
                    .toExternalForm());
    
    0 讨论(0)
  • 2020-12-06 06:02

    Have you initialized the scene object prior to setting style sheet?

    scene = new Scene(myRootLayout, 600, 600); // for example

    0 讨论(0)
  • 2020-12-06 06:02

    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());
    
    0 讨论(0)
  • 2020-12-06 06:02

    In a maven project you must define path for resources in the file.pom in the javafx-plugin section: something like this:

    <configuration>
        <mainClass>com.personale.ciaomondo.App</mainClass>
              <resources>          
                <resource>
                  <directory>src/main/resources</directory>
                </resource>
              </resources>
    </configuration>
    
    0 讨论(0)
  • 2020-12-06 06:03

    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());
    
    0 讨论(0)
  • 2020-12-06 06:06

    put your yourname.css file directly under src directory.

    scene.getStylesheets().add("yourname.css")
    

    clean and build required

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