本文介绍了JavaFX:如何“重绘”一个tableview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在麻烦...我有一个JavaFX Tableview的cointain,在一列,一些结果。这些结果可以是OK,N / A和KO,如果我有一个OK我画它为绿色,如果我有一个N / A KO我绘制它的红色(所有通过方法setStyle())。问题是,当我滑动表垂直的文本的颜色随机变化,我有红色的OK或绿色的KO...我想我应该使用像repaint(),但JavaFX没有它,那么我该怎么办呢?关于结果的代码:

I am in troubles... I have a JavaFX Tableview tha cointain, in a column, some results. These results can be "OK", "N/A" and "KO" and if i have an "OK" i paint it in green, if i have a "N/A" i paint in black,if i have a "KO" i paint it in red (all by method setStyle()). The problem is that when i slide the table vertically the colour of the text change randomly and i have "OK" in red or "KO" in green... I think i should use something like repaint() but JavaFX hasn't it, so how can i do? The code about the results:

for (ResultMatch result : events) {

        isMatch = (result.match().equals("OK") || result.match().equals("N/A"));

        //Set the style
        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);
                            switch (item) {
                                case "OK":
                                    getStyleClass().remove("koItem");
                                    getStyleClass().remove("naItem");
                                    getStyleClass().add("okItem");
                                    break;
                                case "N/A":
                                    getStyleClass().remove("okItem");
                                    getStyleClass().remove("koItem");
                                    getStyleClass().add("naItem");
                                    break;
                                case "KO":
                                    getStyleClass().remove("okItem");
                                    getStyleClass().remove("naItem");
                                    getStyleClass().add("koItem");
                                    break;
                                default:
                                    setStyle("");
                                    break;
                            }


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


            }
        });
        isPass = isPass && isMatch;
        reader.getSampleController().getViewXML().getItems().add(result);
    }


推荐答案

在添加前需要的样式类。而且,@brian在评论中说,在默认情况下,删除所有的样式类。

In each case, remove all the style classes before adding the one you need. And, as @brian says in the comments, in the default case, remove all the style classes.

原因是样式类表示为 List< String> ,因此它可以包含重复的值。 remove(...)方法只删除一个副本。在 updateItem(...)方法中尝试 System.out.println(getStyleClass()); 可能看到建立的类列表。

The reason is that the style class is represented as a List<String>, so it can contain duplicate values. The remove(...) method only removes one copy. Try System.out.println(getStyleClass()); in the updateItem(...) method and you will likely see the list of classes building up.

我会这样做:

final List<String> allStyleClasses = Arrays.asList("koItem", "naItem", "okItem");

// ...
                    @Override
                    public void updateItem(final String item, final boolean empty) {
                        super.updateItem(item, empty);
                        if (item != null) {
                            setText(item);
                            setAlignment(Pos.CENTER);
                            getStyleClass().removeAll(allStyleClasses);
                            switch (item) {
                                case "OK":
                                    getStyleClass().add("okItem");
                                    break;
                                case "N/A":
                                    getStyleClass().add("naItem");
                                    break;
                                case "KO":
                                    getStyleClass().add("koItem");
                                    break;
                                default:
                                    break;
                            }


                        } else {
                            setText(null);
                        }
                    }

这篇关于JavaFX:如何“重绘”一个tableview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 08:20