bind Style Sheet To JavaFX Window - Java JavaFX

Java examples for JavaFX:CSS

Description

bind Style Sheet To JavaFX Window

Demo Code


    //package com.java2s;

    import javafx.beans.property.StringProperty;

    import javafx.stage.Window;

    public class Main {
        public static void bindStyleSheetToWindow(Window window, StringProperty stylesheet) {
    window.sceneProperty().addListener(e -> {
        if (window.getScene() != null) {
            window.getScene().getStylesheets().add(stylesheet.get());
        }//w  w w.  j a  va2s.c  o m
    });
    if (window.getScene() != null) {
        window.getScene().getStylesheets().add(stylesheet.get());
    }

    stylesheet.addListener((obs, o, n) -> {
        if (window.getScene() != null) {
            int oldPos = -1;
            if (o != null) {
                oldPos = window.getScene().getStylesheets().indexOf(o);
                window.getScene().getStylesheets().remove(o);
            }
            if (n != null) {
                if (oldPos >= 0) {
                    window.getScene().getStylesheets().add(oldPos, n);
                } else {
                    window.getScene().getStylesheets().add(n);
                }
            }
        }
    });
}
    }

Related Tutorials