Javafx Gridpane Cell Background Colors


So I have a javafx program which procedurally creates text nodes and inserts them into a GridPane. Each of these text nodes is given a CSS class. I used a stylesheet, to try to make the cells of each class a different color. However, I can only use the -fx-fill property to change the text color, which is good, but I cannot find anything online as to how to change the background of each cell through CSS or Java. I have also tried the fx-background-color property in the CSS, but that doesn't work. Any help would be appreciated; thanks.

8/1/2014 12:18:29 AM

Accepted Answer

You need to use a Region, or some subclass of Region, in order to set the background color.


Use a Label instead of a Text. You can set the text color with -fx-text-fill and the background color with -fx-background-color.


Create a StackPane for each Text; add the Text to the StackPane and the StackPane to the GridPane. You can set the text color with -fx-fill on the Text and the background color with -fx-background-color on the StackPane. You will probably need to call

GridPane.setFillHeight(myStackPane, true);
GridPane.setFillWidth(myStackPane, true);

on each StackPane to get the StackPane to fill the GridPane cell.

8/1/2014 1:27:58 AM

You can use stackpane as well change background color of stackpane. Add the node of single cell in stackpane and after you need to add stackpane in grid


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow