How to attach a CSS stylesheet to FXML?


Tags: css,javafx,fxml

Problem :

Following Oracle tutorial http://docs.oracle.com/javafx/2/get_started/fxml_tutorial.htm, 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="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2" fx:controller="fxmlexample.FXMLExampleController" styleClass="root" >
...other stuff...
<stylesheets>
    <URL value="@Login.css" />
</stylesheets>
</GridPane>

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(LauncherImpl.java:894)
    at com.sun.javafx.application.LauncherImpl.access$000(LauncherImpl.java:56)
    at com.sun.javafx.application.LauncherImpl$1.run(LauncherImpl.java:158)
    at java.lang.Thread.run(Thread.java:745)
Caused by: javafx.fxml.LoadException: URL is not a valid type.
file:/C:/Users/User1/Documents/NetBeansProjects/FXMLExample/dist/run1970052180/FXMLExample.jar!/fxmlexample/fxml_example.fxml:46

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?



Solution :

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

<?import java.net.*?>

    CSS Howto..

    css/html how to make logo appear after heading

    Shouldn't adapting the scree size of a fluid layout in the desktop show how the site should look for mobile?

    How can I zoom this gallery without moving the other pictures?

    How to make a button CSS change only IF form fields are valid

    CSS - how to make content fit to the width/height of paragraph?

    how to change a css of an input if the value of it is not empty in angularJS

    How to capitalize first line with css? ::first-line psuedo-element CSS not working

    How to reset css before repeating sequence

    How to apply class only for IE?

    AngularJS / CSS - How to set the width of a select box to a value from $scope?

    How to set overflow content to go underneath instead of fitting in one row

    How to assign a Css class on an item based on click

    Meteor: how to style useraccounts

    How to create a CSS shade effect with a faded sidebar

    How to fade the background-image to black with CSS?

    How to keep the underline effect in this demo stay using CSS?

    How to make a div align with a specific td on the page?

    How to create Two Columns with Separate Scrollbars in Responsive Bootstrap?

    Weird css box-sizing in bootstrap 3. How to get rid of horizontal slider

    How to set backround image in css

    How to fill Frameless grid dynamically?

    How to horizontally stack elements using CSS?

    How to style multiples of 3 li elements [duplicate]

    How to make a div box with display:flex clickable like display:block?

    Drag and drop in AngularJS: make a directive that is a drop zone, showing an overlay when dragging

    How do I change the color of Semantic UI icons?

    How to use Knockout.js to change a CSS class on mouseover?

    Textarea doesn't show newline in IE

    How to load CSS for different URL in spring MVC app

    How to target individually nested elements using CSS