JavaFX: how to “repaint” a tableview

余生颓废 提交于 2019-12-13 03:48:50

问题


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

回答1:


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.

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.

I would do:

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


来源:https://stackoverflow.com/questions/24259231/javafx-how-to-repaint-a-tableview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!