Abgehter Abgehter - 1 year ago 102
CSS Question

Java Fx Userchoosen color CSS

I built a Java Swing based Game (Reversi aka Othello, student-project).
Via ColorChooser in the MenuBar i gave the User the possibility to choose individual colors for the background, the court and the players.

this.menu_bar.bg_color.addActionListener(e -> { Color buffer = colors[0];
colors[0] = JColorChooser.showDialog(null, "Hintergrundfarbe", colors[0]);
if (colors[0] == null)
colors[0] = buffer;

The used colors are saved in the array "colors", along some methodcalls the choosen color gets set.

void change_colors(Color[] colors)
this.colors = colors;

All works fine.

Now Im buiding an Java Fx equivalent. I dont use FXML or Scene Builder, but I try to style with CSS-file.


-fx-background-color: rgb(34, 139, 34);

My question: Is there a css-based way to get the same effect possible, to set an individual color set for my components? For example use variables in the .css or something like this?

Answer Source

You can use looked-up colors (scroll down beyond the color squares for the description of looked-up colors):


.root {
    /* default value */
    -cell-background: rgb(34, 139, 34);
.cell {
    -fx-background-color: -cell-background ;



// ...

int r = ... ;
int g = ... ;
int b = ... ;
this.setStyle(String.format("-cell-background: rgb(%d, %d, %d);", r, g, b));
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download