0

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);
    }
Peppalvino
  • 23
  • 1
  • 6
  • This is a duplicate of http://stackoverflow.com/questions/11065140/javafx-2-1-tableview-refresh-items but the solution posted on that question did not work for me so I think either that question should be updated or this should be answered. – Cobbles Jun 17 '14 at 08:41
  • I have looked around for some answers but i can't find a solution around here so i decided to post this question :) Hope it could help... – Peppalvino Jun 17 '14 at 09:09
  • Maybe your default should remove all the style classes instead of setStyle("") – brian Jun 18 '14 at 01:54

1 Answers1

0

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);
                        }
                    }
James_D
  • 201,275
  • 16
  • 291
  • 322
  • It works!!! Thank you very much James!!! I have only another little problem... My lists are updated dinamically... Sometimes the first element must change from OK to N/A and when this appens the only way to update the graphic is to scroll down and then scroll up again because only when i hide the element the graphic changes... Solutions? – Peppalvino Jun 18 '14 at 09:05
  • That's a different question, and probably already answered elsewhere on the site. – James_D Jun 18 '14 at 11:39