JavaFX-Specify the location of the CSS file relative to the fxml file

Basic implementation

Folder structure


`-src/main/
  |-java/
  | `-sample/javafx/
  |   `-Main.java
  |
  `-resources/
    |-main.fxml
    `-main.css

main.css


.label {
    -fx-font-size: 25pt;
    -fx-text-fill: yellow;
    -fx-underline: true;
    -fx-padding: 10px;
    -fx-background-color: skyblue;
    -fx-font-family: consolas;
}

Main.java


package sample.javafx;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class Main extends Application {
    public static void main(String[] args) {
        Application.launch(args);
    }
    
    @Override
    public void start(Stage primaryStage) throws Exception {
        FXMLLoader loader = new FXMLLoader(this.getClass().getResource("/main.fxml"));
        
        Parent root = loader.load();
        Scene scene = new Scene(root);
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}

When set normally

main.fxml


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

<?import javafx.scene.control.Label?>

<Label stylesheets="main.css" text="Style Sheet" xmlns="http://javafx.com/javafx/8.0.121" xmlns:fx="http://javafx.com/fxml/1" />

Execution result

javafx.jpg

The style sheet is reflected properly.

** When viewed in Scene Budiler **

javafx.jpg

The style sheet settings are not reflected.

Description

main.fxml


<Label stylesheets="main.css" ... />

--To specify a CSS file for a node Specify the path of the CSS file in the stylesheets attribute --This path will be ** relative to the root of the classpath **

getStyleSheets() | Scene (JavaFX 8)

The URL is a hierarchical URI in the form [scheme:] [// authority] [path]. If the URL does not have a [scheme:] component, the URL is considered to be the [path] component only. ** All "/" characters before [path] are ignored and [path] is treated as a relative path to the root of the application's classpath. ** **

--That is, at runtime you can find main.css from the root of the classpath so the style is applied correctly --But when viewed in Scene Builder, the style is not applied because main.css cannot be found in the Scene Builder classpath. ――In this case, the merit of creating a screen graphically with Scene Builder has been lost.

Set it to look the same in Scene Builder

main.fxml


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

<?import javafx.scene.control.Label?>

<Label stylesheets="@main.css" text="Style Sheet" xmlns="http://javafx.com/javafx/8.0.121" xmlns:fx="http://javafx.com/fxml/1" />

Execution result

javafx.jpg

** When viewed in Scene Budiler **

javafx.jpg

The style is also applied when viewed in Scene Builder.

Description

main.fxml


<Label stylesheets="@main.css" ... />

--Specify the path specification of stylesheets to start with@ --Then, this path will be treated as ** relative to the fxml file **

Location resolution|Overview of FXML| JavaFX 8.0

** The location resolution operator (represented by the @ prefix to the attribute value) ** makes the attribute value a ** relative location relative to the current file, rather than a simple string. Used to specify ** what needs to be processed.

--As a result, you can find main.css even from the viewpoint of Scene Builder, and the style will be applied.

Recommended Posts

JavaFX-Specify the location of the CSS file relative to the fxml file
How to change the contents of the jar file without decompressing
Add Extended Attributes to the file
It doesn't respond to the description in .js of the packs file
Replace the contents of the Jar file
Fix the file name of war to the one set in Maven
[chown] How to change the owner of a file or directory
I made a tool to output the difference of CSV file
How to get the length of an audio file in java
How to decorate the radio button of rails6 form_with (helper) with CSS
Procedure to make the value of the property file visible in Spring Boot
How to find out the Java version of a compiled class file
[Java] How to use the File class
The secret to the success of IntelliJ IDEA
How to delete the wrong migration file
How to delete the migration file NO FILE
How to determine the number of parallels
How to sort the List of SelectItem
JavaFX application file structure (FXML, CSS, properties)
Output of the book "Introduction to Java"
Try changing the .erb file to .slim
The process of introducing Vuetify to Rails
How to mount the batch file location via WSL2 and start the Docker container
How to set environment variables in the properties file of Spring boot application
How to find the cause of the Ruby error
JAVA: jar, aar, view the contents of the file
I want to output the day of the week
[Rails] Button to return to the top of the page
Add empty data to the top of the list
Customize how to divide the contents of Recyclerview
Make a margin to the left of the TextField
The story of introducing Ajax communication to ruby
Set the time of LocalDateTime to a specific time
The story of raising Spring Boot 1.5 series to 2.1 series
I want to var_dump the contents of the intent
Change the timezone of the https-portal container to JST
Rename the package name of the existing jar file
Output of how to use the slice method
[Ruby] Code to display the day of the week
Double-click to open the jar file on Windows
The story of adding the latest Node.js to DockerFile
How to display the result of form input
Change the location folder of Docker image & container
[Java] How to get the authority of the folder
[Spring Boot] How to refer to the property file
Java Welcome to the Swamp of 2D Arrays
If you use SQLite with VSCode, use the extension (how to see the binary file of sqlite3)
Sample code to assign a value in a property file to a field of the expected type