Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/382.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何分别为javafx TableView中的每一行设置颜色?_Java_Javafx - Fatal编程技术网

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

如何分别为javafx TableView中的每一行设置颜色?,java,javafx,Java,Javafx,我想根据每行中的单元格设置每行的颜色。 我尝试了很多解决方案,但似乎都不管用。我最终得到了类似这样的结果,但我无法在这个lambda表达式中更改getCellObservableValue的索引参数 storageProductTableView.setRowFactory(param -> { LocalDate currentDate = LocalDate.now(); TableRow<StorageProduct> ro

我想根据每行中的单元格设置每行的颜色。 我尝试了很多解决方案,但似乎都不管用。我最终得到了类似这样的结果,但我无法在这个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;
    });
storageProductTableView.setRowFactory(参数->{
LocalDate currentDate=LocalDate.now();
TableRow行=新TableRow();
字符串tempExpirationDate=expirationDateColumn.getCellObservableValue(0.getValue();
LocalDate expirationDate=LocalDate.parse(模式的tempExpirationDate、DateTimeFormatter.of(“yyyy-MM-d”);
int difference=Period.between(currentDate,expirationDate).getDays();
如果(差异<0){
row.getStyleClass().add(“过期行”);
}否则,如果(0
我想根据该行中的单元格设置每行的颜色

和fabiancomment,您可以使用
回调从
updateItem()
执行此操作。我制作此示例是为了满足您的需要:

/*
 * 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);
    }
}
/*
*要更改此许可证标题,请在“项目属性”中选择“许可证标题”。
*要更改此模板文件,请选择工具|模板
*然后在编辑器中打开模板。
*/
包装行;
导入java.net.URL;
导入java.util.ResourceBundle;
导入javafx.fxml.fxml;
导入javafx.fxml.Initializable;
导入javafx.scene.control.TableCell;
导入javafx.scene.control.TableColumn;
导入javafx.scene.control.TableRow;
导入javafx.scene.control.TableView;
导入javafx.scene.control.cell.PropertyValueFactory;
导入javafx.util.Callback;
/**
*
*@作者电子
*/
公共类FXMLDocumentController实现可初始化{
@FXML
专用桌面视图按钮;
@FXML
私有表列名称;
@FXML
专用表列颜色;
@凌驾
公共void初始化(URL、ResourceBundle rb){
填充();
styleRowColor();
}
私有void styleRowColor(){
回叫手机工厂
= //
新回调函数(){
@凌驾
公共TableCell调用(最终TableColumn参数){
最终TableCell单元格=新TableCell(){
@凌驾
public void updateItem(字符串项,布尔值为空){
super.updateItem(项,空);
if(空){
设置图形(空);
setText(空);
}否则{
setText(项目);
TableRow行=getTableRow();
如果(row.getItem().getColor().equals(“红色”)){
row.getStyleClass().clear();
row.getStyleClass().add(“红色行”);
}
如果(row.getItem().getColor().equals(“橙色”)){
row.getStyleClass().clear();
row.getStyleClass().add(“橙色行”);
}
如果(row.getItem().getColor().equals(“绿色”)){
row.getStyleClass().clear();
row.getStyleClass().add(“绿行”);
}
如果(row.getItem().getColor().equals(“黄色”)){
row.getStyleClass().clear();
row.getStyleClass().add(“黄色行”);
}
}
}
};
返回单元;
}
};
颜色。setCellFactory(cellFactory);
}
私有void填充(){
名称。setCellValueFactory(新属性ValueFactory(“名称”);
color.setCellValueFactory(新属性ValueFactory(“颜色”));
按钮按钮=新按钮(“btn1”、“红色”);
按钮2=新按钮(“btn2”,“绿色”);
按钮按钮3=新按钮(“btn3”、“黄色”);
按钮按钮4=新按钮(“btn4”、“橙色”);
buttons.getItems().addAll(按钮、按钮2、按钮3、按钮4);
}
}
结果是:


a)从
TableRow
updateItem
updateIndex
方法执行此操作记住删除旧的样式类;否则您可能会得到一个包含多个样式类的行。c)如果属性是可观察的,并且可能会更改,您需要向属性添加一个侦听器以获得更改通知。您是否将css文件添加到场景中?这就是我要查找的内容。