Arkhan6
Arkhan6

Reputation: 157

TableView ObservableList change row style

I need to change the color of the rows that have in column 6 the letter 'N', I have seen many examples but not with ObservableList and I need to do it this way.

In the 'FOR' I control the size of the table and in the 'IF' I check that all rows that contain the letter 'N' in column 6 change the entire row color.

@FXML private TableView<ObservableList> table;  

        for(int i = 0; i< table.getItems().size(); i++){
                            if(table.getItems().get(i).get(6).toString().equalsIgnoreCase("N")){
                                //table.get.get(i).setStyle("-fx-background-color: yellow"); //NOT FOUND
                            }
        }

Upvotes: 1

Views: 567

Answers (1)

James_D
James_D

Reputation: 209714

Use a row factory on the table:

public class MyControllerClass {

    @FXML
    private TableView<ObservableList<Object>> table ; // TODO: use a more appropriate type than Object

    public void initialize() {
        table.setRowFactory(tv -> new TableRow<ObservableList<Object>>() {
            @Override
            protected void updateItem(ObservableList<Object> row, boolean empty) {
                super.updateItem(row, empty);
                if (row != null && row.get(6).toString().equalsIgnoreCase("N")) {
                    setStyle("-fx-background-color: yellow;");
                } else {
                    setStyle("");
                }
            }
        });
    }

    // ...
}

Upvotes: 2

Related Questions