Add css to JavaFX node - Java JavaFX

Java examples for JavaFX:CSS

Description

Add css to JavaFX node

Demo Code


import static java.util.logging.Logger.getLogger;
import java.net.URL;
import java.util.logging.Logger;
import javafx.application.Platform;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.layout.ColumnConstraints;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Priority;
import javafx.scene.layout.Region;
import javafx.scene.layout.RowConstraints;
import javafx.scene.web.HTMLEditor;
import javafx.stage.Screen;
import javafx.stage.Stage;

public class Main{
    private final static Logger LOGGER = getLogger(GUIUtils.class.getName());
    /**//from  w w w .  j  a  v a2s.co  m
         * Add css to node
         *
         * @param node
         * @param path path to css from resources root directory
         */
        public static void addCss(Region node, String path) {

            URL resource = GUIUtils.class.getClassLoader().getResource(path);
            if (resource != null) {
                node.getStylesheets().add(resource.toExternalForm());
            } else {
                LOGGER.severe("Failed to load " + path);
            }

        }
    /**
         * Add css to scene
         *
         * @param node
         * @param path path to css from resources root directory
         */
        public static void addCss(Scene node, String path) {
            URL resource = GUIUtils.class.getClassLoader().getResource(path);
            if (resource != null) {
                node.getStylesheets().add(resource.toExternalForm());
            } else {
                LOGGER.severe("Failed to load " + path);
            }

        }
}

Related Tutorials