Javafx: TableView change row color based on column value

后端 未结 4 1130
生来不讨喜
生来不讨喜 2020-12-05 16:28

I have the following piece of code to update both the color of a column cell and its corresponding row:

    calltypel.setCellFactory(column -> {
        r         


        
4条回答
  •  忘掉有多难
    2020-12-05 17:21

    private void customiseFactory(TableColumn calltypel) {
        calltypel.setCellFactory(column -> {
            return new TableCell() {
                @Override
                protected void updateItem(String item, boolean empty) {
                    super.updateItem(item, empty);
    
                    setText(empty ? "" : getItem().toString());
                    setGraphic(null);
    
                    TableRow currentRow = getTableRow();
    
                    if (!isEmpty()) {
    
                        if(item.equals("a")) 
                            currentRow.setStyle("-fx-background-color:lightcoral");
                        else
                            currentRow.setStyle("-fx-background-color:lightgreen");
                    }
                }
            };
        });
    }
    

    This works!

提交回复
热议问题