Here is my JavaFX code snippet:
<stylesheet value="colours.css" />
This is inside a GridPane object, right after the line with the GridPane tag.
IntelliJ says it is unable to compile the symbol "stylesheet" and the value attribute is not allowed, I also get a very long error when I try to run it.
I tried looking through SO and searching online (I'm not very good at using javaFX), and changed the "stylesheet value" to other things a few times (I don't exactly remember what), but nothing actually worked.
The property to which you're trying to set a value is a "read only list property" called stylesheets
(not stylesheet
).
What this means is the property maps to a method (defined in the Parent
class) called getStylesheets()
, which returns a List
and has no matching setStylesheets()
method. (This makes sense, as you have have multiple stylesheets for a single FXML element.) The way this is handled by the FXMLLoader
is to add the value(s) you specify for the property to the list.
Defining the correct path in FXML for the CSS file can be a little tricky. You can use something like
<stylesheets>
<URL value="@colours.css"/>
</stylesheets>
This assumes the CSS file is in the same package as the FXML file. See the documentation on location resolution for more details.
Note that you can also specify the CSS as a property attribute, instead of a property instance, which may be easier:
<GridPane stylesheets="@colours.css">
<!-- ... -->
</GridPane>