更改TableView中单个数据单元格的颜色 [英] Change the color of a single data cell in a TableView

查看:384
本文介绍了更改TableView中单个数据单元格的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在表视图中有一列我想要:
绿色字体,如果我写OK,红色字体,如果我写KO。
问题是我尝试在方法setCellFactory中修改此值,但它不起作用,因为String具有所有相同颜色(红色或绿色),即最后一个String的颜色...对于istance,如果我的最后一个值是KO,我列中的所有字符串都将为红色。
我该怎么办?
感谢您的帮助!

I have a column in a table view and i want: Green font if i write "OK", red font if i write "KO". The problem is that i try to modify this value in the method "setCellFactory" but it doesn't work because the String have all the same color (red or green) that is the color of the last String... For istance if my last value is "KO" all the String in my column will be red. How can i do? Thank you for the help!

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;
            }
        });


推荐答案

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

So...The final code is:

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);
                    }
                }
            };


        }
    });

这篇关于更改TableView中单个数据单元格的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆