0

enter image description here

So this is the file structure im working with, I have the app.java as my main java file and I tried these two lines of code to no avail

scene.getStylesheets().add("mainpage.css");
scene.getStylesheets().add(getClass().getResource("mainpage.css").toExternalForm());

In the xml file I have:

<stylesheets>
    <URL value="@mainpage.css" />
</stylesheets>`

not sure why it cant find the css, but the output keeps trying to point to the same location in the file system and I dont have a path set for my css file set anywhere

Slaw
  • 37,820
  • 8
  • 53
  • 80
  • I'm assuming your class containing the scene is in `/src/main/java/com/mycompany/catalyst` ? What is `but the output keeps trying to point to the same location in the file system` ? Try adding a `/` like so, `scene.getStylesheets().add(getClass().getResource("/mainpage.css").toExternalForm());` Also, if you're adding the stylesheet to the scene, you don't need to also add it via the FXML – b3tuning Jun 23 '20 at 06:45
  • Thank you for pointing out that my fxml was defining the style sheet as well, thats what was causing the issue, I just removed it from the fxml and everything worked properly. Is it preferred to add it to the scene or to add it to the fxml and is there a difference? – andrewaleks Jun 23 '20 at 20:21
  • Adding stylesheet to a scene is also applied to all children of the scene. Adding it to an .fxml is applied to that .fxml only and will usually override a stylesheet from the parent (or scene) and as you found out can cause issues when it can't find the stylesheet. My preference is to load an app wide style sheet to the primary stage, and if I want certain nodes to have a special style applied, I have special selectors in the style sheet and add those selectors to the node in the controller. – b3tuning Jun 24 '20 at 03:33
  • When I want certain styles for panes or container nodes, I again use custom selectors but assign them in the .fxml (ie: `AnchorPane styleClass="white-background-with-border"`). Again, this is just my preference, do what makes sense to you, but stay consistent – b3tuning Jun 24 '20 at 03:35

0 Answers0