css - 更改 TableView 中单个数据单元格的颜色

标签 css javafx tableview cell fxml

我在 TableView 中有一列,我想要: 如果我写“OK”,则为绿色字体;如果我写“KO”,则为红色字体。 问题是我尝试在方法“setCellFactory”中修改此值,但它不起作用,因为字符串具有与最后一个字符串的颜色相同的颜色(红色或绿色)......如果我的最后一个值是“KO”,我列中的所有字符串都将是红色的。 我能怎么做? 感谢您的帮助!

reader.getSampleController().xmlMatch.setCellFactory(new Callback<TableColumn, TableCell>() {
            @Override
            public TableCell call(TableColumn param) {
                TableCell cell = new TableCell() {
                    @Override
                    public void updateItem(Object item, boolean empty) {
                        if (item != null) {
                            setText(item.toString());
                        }

                    }
                };

                cell.setAlignment(Pos.CENTER);

                if (result.match().equals("OK")) {
                    cell.setStyle("-fx-text-fill: green; -fx-font-weight:bold;");
                } else if (result.match().equals("N/A")){
                        cell.setStyle("-fx-text-fill: black; -fx-font-weight:bold;");
                }else{
                    cell.setStyle("-fx-text-fill: red; -fx-font-weight:bold;");
                }

                return cell;
            }
        });

最佳答案

所以...最终的代码是:

reader.getSampleController().xmlMatch.setCellFactory(new Callback<TableColumn<String, String>, TableCell<String, String>>() {
        @Override
        public TableCell call(TableColumn p) {
            return new TableCell<String, String>() {
                @Override
                public void updateItem(final String item, final boolean empty) {
                    super.updateItem(item, empty);
                    if (item != null) {
                        setText(item);
                        setAlignment(Pos.CENTER);
                        //setStyle("");

                        if (item.equals("OK")) {
                            setStyle("-fx-text-fill: green; -fx-font-weight:bold;");
                        }
                        else if (item.equals("N/A")) {
                            setStyle("-fx-text-fill: black; -fx-font-weight:bold;");
                        }
                        else if (item.equals("KO") ) {
                            setStyle("-fx-text-fill: red; -fx-font-weight:bold;");
                        }
                        else setStyle("");


                    } else {
                        setText(null);
                    }
                }
            };


        }
    });

关于css - 更改 TableView 中单个数据单元格的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23872477/

相关文章:

javascript - 如何制作基本形状CSS悬停效果?

javascript - 检索数据时将空格应用于输入框

javafx - 在javafx中清除HBox

ios - ContiguousArrayStorage 行无法识别的选择器发送到实例

iphone - UITableView 函数 visibleCells 是否返回自定义单元格数据?

html - 如何使用大于可见区域的背景图像

javascript - 使用js修改外部css值

java - FXML 声明的自定义控件无法在初始化时访问声明的字段

java - 有没有办法修复这个无效的模块名称错误?

ios - 自定义 UITableView 单元格图像转换不适用于可重用单元格