Javafx: TableView change row color based on column value

后端 未结 4 1127
生来不讨喜
生来不讨喜 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:12

    I recently did a little research about this subject. With the following code you can change the row color of a TableView based on a column value (I will try to explain it the best I can).

    The first thing we have to do is to define the TableView and the Columns of this TableView:

    private TableView personTable;
    private TableColumn nameColumn;
    private TableColumn lastNameColumn;
    

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

    nameColumn.setCellFactory(column -> {
        return new TableCell() {
            @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);
                    }
                }
            }
        };
    });
    

    The logic of the code: the updateItem() method that we overwrite, it's called automatically when the underlying item changes.

    We receive the data item (a String in this case) that has to be rendered. If the item is empty or null (an empty cell for example), we don't apply any style. Otherwise, we format the item, set the text of the cell, and also the colour and the background, depending on the Name of the Person.

    If you want to apply this colour of the cell in the other columns of the table, we have to use 'Row Factory' instead of 'Cell Factory', but the logic of the code is similar:

    personTable.setRowFactory(row -> new TableRow(){
        @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

    This is the best way I found to apply the change of style in all the cells of the row. If you use the method "getTableRow()" inside the Cell Factory, you can't modify their cell children.

    NOTE 1: If you want to change the style of the text, you have to work in the cell. If you try to do this changes directly on the row, has no effect.

    NOTE 2: If you are using a separated CSS file, don't write something like this:

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

    Because if you do this, all the Java code has no effect.

提交回复
热议问题