颜色表单元格取决于单元格数据

时间:2019-06-12 06:45:51

标签: java javafx

我在Internet上找到了一个很好的示例,说明如何执行此操作,实际上这是行不通的。下面是带有我的注释的代码,请帮助找出什么是什么以及为什么不起作用。

    @FXML
    private void initialize() {
timeContractColumn.setCellFactory(column -> {
            return new TableCell<MainData, LocalDate>() { // MainData - model,
// where all variables and collections for storing data are stored
// timeContractColumn - stores the entered date, therefore, LocalDate
                @Override
                protected void updateItem(LocalDate item, boolean empty) {
                    super.updateItem(item, empty);

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

                        setText(item.toString()); //We place the data in the cell
                        System.out.println(item);

                        // We get here all the information about this line.
                        MainData auxPerson = getTableView().getItems().get(getIndex());
                        System.out.println(auxPerson.toString());

                        // Change the style if ...
                        if (auxPerson.getTimeContract().equals("2019-04-09")) {
                            setTextFill(Color.RED);
                            setStyle("-fx-background-color: yellow");
                        } else {
                            //Here we see whether the row of this cell is highlighted or not
                            if(getTableView().getSelectionModel().getSelectedItems().contains(auxPerson))
                                setTextFill(Color.WHITE);
                            else
                                setTextFill(Color.BLACK);
                        }
                    }
                }
            };
        });
}

1 个答案:

答案 0 :(得分:0)

Fabian是正确的,我没有考虑到我在比较不同的对象,谢谢所有的建议。

if (auxPerson.getTimeContract().toString().equals("2019-04-11"))