Search code examples
javajavafxcolorstableviewcell

How to color a row of a TableView?


I am trying to understand how to work with JavaFX but having a hard time understanding on how to color different rows of a tableView in different colors.

I have a scene with a tableview which gets different data depending on radiobuttons. One option displays data without color, the other colors rows depending on how big the number is.

The flawed method:

private void drawTableColor(){

    //tableColumns[0] = new TableColumn("Spieltag");

    //tableColumns[0].setCellValueFactory(new PropertyValueFactory<MyClass, String>("day"));

    /*tableColumns[0].setCellFactory(column -> {
        return new TableCell<MyClass,String>(){
            @Override
            protected void updateItem(String s, boolean empty){
                super.updateItem(s, empty);

                int i = Integer.parseInt(s);
                System.out.println("TEST " +i);
                if (i<=3){
                    setTextFill(Color.BLUE);
                } else if (i==4){
                    setTextFill(Color.AZURE);
                } else if (i <= 6){
                    setTextFill(Color.GREEN);
                } else if (i == 16){
                    setTextFill(Color.ORANGE);
                } else if (i>16){
                    setTextFill(Color.RED);
                }
            }

        };
    });*/

    ObservableList<MyClass> data2 = FXCollections.observableArrayList(data);
    tableView.setItems(null);
    tableView.setItems(data2);
}

The out commented part is giving me trouble. It does not color the row nor does it show any content at all in this column. If the very first line is commented in, it skips the updateItem part. Any idea what I am doing wrong?


Solution

  • The problem is you were using settextfill() Instead you should set the style of the background

    private void drawTableColor(){
    
        //tableColumns[0] = new TableColumn("Spieltag");
    
        //tableColumns[0].setCellValueFactory(new PropertyValueFactory<MyClass, String>("day"));
    
        /*tableColumns[0].setCellFactory(column -> {
            return new TableCell<MyClass,String>(){
                @Override
                protected void updateItem(String s, boolean empty){
                    super.updateItem(s, empty);
    
                    int i = Integer.parseInt(s);
                    System.out.println("TEST " +i);
                    if (i<=3){
                        setStyle("-fx-background-color: blue");
                    } else if (i==4){
                        setStyle("-fx-background-color: azure");
                    } else if (i <= 6){
                        setStyle("-fx-background-color: green");
                    } else if (i == 16){
                        setStyle("-fx-background-color: orange");
                    } else if (i>16){
                        setStyle("-fx-background-color: red");
                    }
                }
    
            };
        });*/
    
        ObservableList<MyClass> data2 = FXCollections.observableArrayList(data);
        tableView.setItems(null);
        tableView.setItems(data2);
    }