Reputation: 23
When I attempted to change the folder where my css files were located, I got this error:
WARNING: com.sun.javafx.css.StyleManager loadStylesheetUnPrivileged Resource "file:/C:/[path-to-project]/Test/resources/css/main.css" not found.
I copied the URL and pasted it into the file manager and it opened the file, so I know it exists.
public static void main(String[] args)
{
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception
{
primaryStage.setTitle("New Window");
Scene scene = new Scene(new AnchorPane(), 800, 600);
primaryStage.setScene(scene);
URL css = new URL("file:///" +
new File("resources/css").getAbsolutePath().replace("\\", "/") +
"/main.css");
scene.getStylesheets().clear();
scene.getStylesheets().add(css.toExternalForm());
primaryStage.show();
}
This is my eclipse project layout [also as an image]:
Test
├───src
│ └───com
│ └───Client.java
├───JRE System Library [JavaSE-1.8]
└───resources
└───css
└───main.css
I have tried:
scene.getStylesheets().add(getClass().getResource("/resources/css/main.css"));
scene.getStylesheets().add(getClass().getResource("resources/css/main.css"));
scene.getStylesheets().add(getClass().getResource("../resources/css/main.css"));
I also tried using fxml to add the css files trying:
stylesheets="@../resources/css/main.css"
stylesheets="@/resouces/css/main.css"
stylesheets="@resources/css/main.css
Upvotes: 2
Views: 15981
Reputation: 27476
Put your main.css
file inside resources/com
directory (so resources
+ package of the class loading it) and then you can use a simple (assuming your class is named Client
and it is in the com
package):
scene.getStylesheets().add(Client.class.getResource("main.css").toExternalForm());
Upvotes: 12