如何为javafx TableView中的每一行单独设置颜色?

时间:2018-01-13 22:18:49

标签: java javafx

我想根据该行中的单元格为每一行设置颜色。 我尝试了很多解决方案,但似乎都没有。我最终得到了类似的东西,但是我无法在这个lambda表达式中更改getCellObservableValue的索引参数。

storageProductTableView.setRowFactory(param -> {
            LocalDate currentDate = LocalDate.now();
            TableRow<StorageProduct> row = new TableRow<>();
            String tempExpirationDate =  expirationDateColumn.getCellObservableValue(0).getValue();
            LocalDate expirationDate = LocalDate.parse(tempExpirationDate, DateTimeFormatter.ofPattern("yyyy-MM-d"));
            int difference = Period.between(currentDate,expirationDate).getDays();
            if(difference < 0){
                row.getStyleClass().add("expired-row");
            } else if(0 < difference && difference<=1){
                row.getStyleClass().add("red-row");
            } else if(1 < difference && difference <=3){
                row.getStyleClass().add("orange-row");
            } else if (3 < difference && difference <= 5) {
                row.getStyleClass().add("yellow-row");
            } else if (difference > 5) {
                row.getStyleClass().add("green-row");
            }

            return row;
    });

1 个答案:

答案 0 :(得分:0)

基于:

  

我想在该行的 on cell 上为每一行设置颜色。

Fabian 评论,您可以使用updateItem()callBack执行此操作。我提供了此示例以满足您的需求:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package row;

import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableRow;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.util.Callback;

/**
 *
 * @author Electron
 */
public class FXMLDocumentController implements Initializable {

    @FXML
    private TableView<Button> buttons;
    @FXML
    private TableColumn<Button, String> name;
    @FXML
    private TableColumn<Button, String> color;

    @Override
    public void initialize(URL url, ResourceBundle rb) {
        populate();
        styleRowColor();
    }

    private void styleRowColor() {
        Callback<TableColumn<Button, String>, TableCell<Button, String>> cellFactory
                = //
                new Callback<TableColumn<Button, String>, TableCell<Button, String>>() {
            @Override
            public TableCell<Button, String> call(final TableColumn<Button, String> param) {
                final TableCell<Button, String> cell = new TableCell<Button, String>() {

                    @Override
                    public void updateItem(String item, boolean empty) {
                        super.updateItem(item, empty);
                        if (empty) {
                            setGraphic(null);
                            setText(null);
                        } else {
                            setText(item);
                            TableRow<Button> row = getTableRow();
                            if (row.getItem().getColor().equals("red")) {
                                row.getStyleClass().clear();
                                row.getStyleClass().add("red-row");
                            }
                            if (row.getItem().getColor().equals("orange")) {
                                row.getStyleClass().clear();
                                row.getStyleClass().add("orange-row");
                            }
                            if (row.getItem().getColor().equals("green")) {
                                row.getStyleClass().clear();
                                row.getStyleClass().add("green-row");
                            }
                            if (row.getItem().getColor().equals("yellow")) {
                                row.getStyleClass().clear();
                                row.getStyleClass().add("yellow-row");
                            }
                        }
                    }
                };
                return cell;
            }
        };
        color.setCellFactory(cellFactory);

    }

    private void populate() {
        name.setCellValueFactory(new PropertyValueFactory<>("name"));
        color.setCellValueFactory(new PropertyValueFactory<>("color"));

        Button button = new Button("btn1", "red");
        Button button2 = new Button("btn2", "green");
        Button button3 = new Button("btn3", "yellow");
        Button button4 = new Button("btn4", "orange");
        buttons.getItems().addAll(button, button2, button3, button4);
    }
}

结果是:

enter image description here