Ras Flor
Ras Flor

Reputation: 11

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

Upvotes: -1

Views: 5589

Answers (2)

faris
faris

Reputation: 702

With the setStyle() method in JavaFX, you can style the default state of a component with your css code in the class code however I don't think you can change the selected state's css. I would recommend just giving the node a unique ID with node.setID("someID") and then working in an actual css file

Upvotes: 0

fabian
fabian

Reputation: 82461

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;");

Upvotes: 2

Related Questions