Let me be clear that I am not trying to use the output of a ColorPicker with this question. I figured out how to do that. I’m just trying to modify it.
By default, the ColorPicker has a white-ish background with a black-ish text color.
The only thing that isn’t default about this is the fact that I’ve assigned the starting color to be black.
Perusing the javadocs has led me to finding that I can change the borders and the background of the ColorPicker just fine:
Color contrast = color.invert();
colorPicker.setBorder(new Border(new BorderStroke(contrast, BorderStrokeStyle.SOLID, null, null)));
colorPicker.setBackground(new Background(new BackgroundFill(color, null, null)));
That gets you the following:
Moved onto a black background so you can see the white border.
Questions like these (JavaFX ColorPicker customizable via CSS) that have images of people having done this before have led me to believe that this is possible. Perhaps they are dated (I’m using javafx v21). However, despite my best efforts, I haven’t manage to accomplish something similar to this using either built-in methods or css through colorPicker.setStyle()
. I’ve tried most of what I can think of aside from making an entire css stylesheet, which I think is overkill (which is my way of implying that I have no idea how to do that, so if you have suggestions for tutorials on how to create css stylesheets, I would appreciate them, since most of the ones I’ve found talk about using css once you’ve set it up instead of how to integrate it into a Java project, which is the part I need a tutorial on because picking up a language is not what I’m confused about with these things).
2
Answers
The style class for the label is
color-picker-label
. You can set the desired color of the label in the css file as below:However, if you are looking to set these colors dynamically, you can create a variable in css for the colors you want. And only update these variables in progrom using
setStyle()
.For demonstration purpose, you can check the below demo code, that updates the background and foreground color of the color picker based on the value it has. I hope this can provide you some direction.
Preface:
Just a summary of my comment above.
Explanation:
Why does your way not work? I hope you are familiar with FXML or at least HTML. There are standalone tags (components) like label or div (HTML), but there are also tags like Colorpicker or media (HTML), which are a summary of many other tags.
A Colorpicker has a Label component inside it (in HTML you would say in its Shadow Dom).
With setStyle() you can only apply CSS to the root element itself (e.g. Colorpicker), but NOT to their child elements (Label).
So you have to create a CSS file in which you CAN specify the styling of child elements.
This
.color-picker{...}
means "put style on ColorPicker.This
.color-picker .color-picker-label {...}
means "put style on all ColorPickerLabels inside ColorPicker".Solution:
Step 1: Create a CSS FILE as String
Step 2: Add the CSS FILE to your Scene
scene.getStylesheets().add(CSS)
;Note:
This
"data:text/css,"
must be the first part of your CSS String. Do not forget it!