繁体   English   中英

如何分别为javafx TableView中的每一行设置颜色?

[英]How to set color to each row in javafx TableView individually?

我想根据该行中的单元格为每行设置颜色。 我正在尝试许多解决方案,但似乎都没有用。 我最终得到了这样的结果,但是我无法在此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;
    });

基于 :

我想根据该行中的单元格为每行设置颜色。

和fabian 注释 ,您可以使用callBackupdateItem()进行此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);
    }
}

结果是:

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM