Javafx: TableView changes row color based on column value

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

    calltypel.setCellFactory(column -> {
        return new TableCell<CallLogs, String>() {
            @Override
            protected void updateItem(String item, boolean empty) {
                super.updateItem(item, empty);

                setText(empty ? "" : getItem().toString());
                setGraphic(null);

                TableRow currentRow = getTableRow();

                //This doesn't work
                if(item.equals("a")){
                    item.setTextFill(Color.RED);
                    currentRow.setTextFill(Color.PINK);
                    }
                else{
                    item.setTextFill(Color.GREEN);
                    currentRow.setTextFill(Color.BLUE);
                }

            }
        };
    });

The "if" condition code segment does not work. I cannot determine the correct object references, and also that this is the best way to do this.

Thank!

+6
source share
3 answers
private void customiseFactory(TableColumn<CallLogs, String> calltypel) {
    calltypel.setCellFactory(column -> {
        return new TableCell<CallLogs, String>() {
            @Override
            protected void updateItem(String item, boolean empty) {
                super.updateItem(item, empty);

                setText(empty ? "" : getItem().toString());
                setGraphic(null);

                TableRow<CallLogs> currentRow = getTableRow();

                if (!isEmpty()) {

                    if(item.equals("a")) 
                        currentRow.setStyle("-fx-background-color:lightcoral");
                    else
                        currentRow.setStyle("-fx-background-color:lightgreen");
                }
            }
        };
    });
}

It works!

+12
source

I recently researched this topic a bit. With the following code, you can change the color of the TableView row based on the column value (I will try to explain this as best as possible).

The first thing we need to do is define the TableView and the columns of this TableView:

private TableView<Person> personTable;
private TableColumn<Person, String> nameColumn;
private TableColumn<Person, String> lastNameColumn;

The next step is to determine the Cell Factory of one of the columns:

nameColumn.setCellFactory(column -> {
    return new TableCell<Person, String>() {
        @Override
        protected void updateItem(String item, boolean empty) {
            super.updateItem(item, empty); //This is mandatory

            if (item == null || empty) { //If the cell is empty
                setText(null);
                setStyle("");
            } else { //If the cell is not empty

                setText(item); //Put the String data in the cell

                //We get here all the info of the Person of this row
                Person auxPerson = getTableView().getItems().get(getIndex());

                // Style all persons wich name is "Edgard"
                if (auxPerson.getName().equals("Edgard")) {
                    setTextFill(Color.RED); //The text in red
                    setStyle("-fx-background-color: yellow"); //The background of the cell in yellow
                } else {
                    //Here I see if the row of this cell is selected or not
                    if(getTableView().getSelectionModel().getSelectedItems().contains(auxPerson))
                        setTextFill(Color.WHITE);
                    else
                        setTextFill(Color.BLACK);
                }
            }
        }
    };
});

: updateItem(), , .

( String), . (, ), . , , , .

, "Row Factory" "Cell Factory", :

personTable.setRowFactory(row -> new TableRow<Person>(){
    @Override
    public void updateItem(Person item, boolean empty){
        super.updateItem(item, empty);

        if (item == null || empty) {
            setStyle("");
        } else {
            //Now 'item' has all the info of the Person in this row
            if (item.getName().equals("Edgar")) {
                //We apply now the changes in all the cells of the row
                for(int i=0; i<getChildren().size();i++){
                    ((Labeled) getChildren().get(i)).setTextFill(Color.RED);
                    ((Labeled) getChildren().get(i)).setStyle("-fx-background-color: yellow");
                }                        
            } else {
                if(getTableView().getSelectionModel().getSelectedItems().contains(item)){
                    for(int i=0; i<getChildren().size();i++){
                        ((Labeled) getChildren().get(i)).setTextFill(Color.WHITE);;
                    }
                }
                else{
                    for(int i=0; i<getChildren().size();i++){
                        ((Labeled) getChildren().get(i)).setTextFill(Color.BLACK);;
                    }
                }
            }
        }
    }
});

. getTableRow() Factory, .

1. , . , , .

2. CSS, - :

.table-cell {
    -fx-text-fill: Black;
}

, , Java .

+4

The correct approach is to use a setRowFactorytable:

table.setRowFactory(tv -> new TableRow<CustomItem>() {
    @Override
    protected void updateItem(CustomItemitem, boolean empty) {
        super.updateItem(item, empty);
        if (item == null || item.getValue() == null)
            setStyle("");
        else if (item.getValue() > 0)
            setStyle("-fx-background-color: #baffba;");
        else if (item.getValue() < 0)
            setStyle("-fx-background-color: #ffd7d1;");
        else
            setStyle("");
    }
});
0
source

All Articles