0

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);
    }    
}
  • Sorry, had to make some changes to the code. – Zach Goniea Nov 20 '19 at 02:16
  • If you want to change the background color of a [JavaFX] `Label`, method `setTextFill()` is ___not___ the way. Have you seen [How to change the color of pane in javafx?](https://stackoverflow.com/questions/22841000/how-to-change-the-color-of-pane-in-javafx_) – Abra Nov 20 '19 at 02:43
  • I've looked at it but I'm not really getting what I need from it. I need to be able to change all the cells like this: https://imgur.com/gallery/nxUF3SG – Zach Goniea Nov 20 '19 at 03:07
  • 1
    `Lbl.setStyle("-fx-background-color:GREEN")` – Abra Nov 20 '19 at 06:57
  • Thank you so much! – Zach Goniea Nov 20 '19 at 13:26

0 Answers0