javafxcheckboxjavafx-9

JavaFX style checkbox


I use this css-script which i include over setStyle:

checkBox.setStyle("" +
            ".check-box:selected > .box {\n" +
            "    /* background color for selected checkbox */\n" +
            "    -fx-background-color: lime;\n" +
            "}\n" +
            "\n" +
            ".check-box > .box {\n" +
            "    /* background color of unselected checkbox */\n" +
            "    -fx-background-color: red;\n" +
            "}\n" +
            "\n" +
            ".check-box:selected > .box > .mark,\n" +
            ".check-box:indeterminate  > .box > .mark {\n" +
            "    /* modify mark color */\n" +
            "    -fx-background-color: blue;\n" +
            "}");

But it doesn't work....

I use Java 9

Thanks for your help


Solution

  • This won't work. The style property can only contain property assignments, not selectors.

    You could assign those colors using a combination of a stylesheet and the style property though by using looked-up colors (only works for colors though):

    CSS stylesheet

    .check-box {
        /* default properties */
        selected-box-color: black;
        box-color: black;
        mark-color: white;
    }
    
    .check-box:selected > .box {
        /* background color for selected checkbox */
        -fx-background-color: selected-box-color;
    }
    
    .check-box > .box {
        /* background color of unselected checkbox */
        -fx-background-color: box-color;
    }
    
    .check-box:selected > .box > .mark,
    .check-box:indeterminate  > .box > .mark {
        /* modify mark color */
        -fx-background-color: mark-color;
    }
    

    Java Code

    // overwrite colors from stylesheet
    checkBox.setStyle("selected-box-color: lime; box-color: red; mark-color: blue;");