How to attach a CSS stylesheet to FXML?


Following Oracle tutorial, I create FXML with a fragment

<?xml version="1.0" encoding="UTF-8"?>

<?language javascript?>

<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>

<GridPane alignment="CENTER" gridLinesVisible="true" hgap="10.0" vgap="10.0" xmlns:fx="" xmlns="" fx:controller="fxmlexample.FXMLExampleController" styleClass="root" >
...other stuff...
    <URL value="@Login.css" />

NetBeans 8.0 colors the URL red and says: "Class does not exist: URL".

When compiled and run, JRE 1.8 also throws an exception:

Caused by: java.lang.RuntimeException: Exception in Application start method
    at com.sun.javafx.application.LauncherImpl.launchApplication1(
    at com.sun.javafx.application.LauncherImpl.access$000(
    at com.sun.javafx.application.LauncherImpl$
Caused by: javafx.fxml.LoadException: URL is not a valid type.

If I open the fxml_example.fxml file in JavaFX Scene Builder 1.1", it also complains about missing types, elements of which will not be shown:

Missing types are: [URL]

Everyone seems to be using <URL/> elements, why cannot I?

5/5/2014 3:03:46 PM

Accepted Answer

URL is a class in the package; you should import it from the FXML:

5/5/2014 3:19:57 PM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow