0

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?

Yo Re
  • 33
  • 1
  • 6
  • You're definitely missing `setText(s)` in your `updateItem` method, and you need (at least) to check the cell is not empty before trying to do `Integer.parseInt(s)` (otherwise you will end up trying to parse a null value). If that still doesn't work, you need to post the code for `MyClass` – James_D Jun 19 '17 at 00:16
  • Thank you, this helped me to fill the column. – Yo Re Jun 20 '17 at 14:09

1 Answers1

1

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);
}
fabian
  • 80,457
  • 12
  • 86
  • 114
Austin
  • 726
  • 4
  • 22