Today I'm trying to figure out how to change the colors of individual cells in a GridPane
. Eventually, If the integer is divisible by 2, I want to color the cell blue. If the integer is divisible by 3, I want to color the cell yellow. If the integer is divisible by 6, I want to color the cell green. But right now I want to figure out how to change the color of individual backgrounds of buttons.
Here's what I've got:
public class GridPaneWithRNG extends Application {
@Override
public void start(Stage primaryStage) {
int SIZE = 10;
int length = SIZE;
int width = SIZE;
GridPane root = new GridPane();
for(int y = 0; y < length; y++){
for(int x = 0; x < width; x++){
Random rand = new Random();
int rand1 = rand.nextInt(100);
Label Lbl = new Label();
Lbl.setPrefHeight(100);
Lbl.setPrefWidth(90);
Lbl.setAlignment(Pos.CENTER);
Lbl.setText("" + rand1);
GridPane.setRowIndex(Lbl,y);
GridPane.setColumnIndex(Lbl,x);
root.getChildren().add(Lbl);
if (rand1 % 2 == 0) {
Lbl.setTextFill(Color.AQUA);
}
if (rand1 % 3 == 0) {
Lbl.setTextFill(Color.RED);
}
if (rand1 % 6 == 0) {
Lbl.setTextFill(Color.BLUEVIOLET);
}
}
}
Scene scene = new Scene(root, 500, 500);
primaryStage.setTitle("GridPaneWithRNG");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}