简体   繁体   中英

Fail showing colors in JavaFX Table

I´ve developed an app in Java-JavaFX. It works perfectly on several comoputers(about 20),, but there is one that dont shows the color of the rows in the table. All computers are wiindows with several versioins. the one which fails is a Winndows 7 64bit. Here is my code

public TableRow<Orden> call(TableView<Orden> tableView) {

    final TableRow<Orden> row = new TableRow<Orden>() {
        @Override
        protected void updateItem(Orden orden, boolean empty){
            super.updateItem(orden, empty);

            if(null!=orden) {
                clearPriorityStyle();
                String color = calcularColorRow(orden);
                if(!color.equals("")){
                    this.getStyleClass().add(color);

                }

            }

        }

        private String calcularColorRow(Orden o){
            if(null!=o.getmColorCode()){
                Integer colorCode = o.getmColorCode();
                switch(colorCode){
                case 0:
                    return "";

                case 1:
                    return "rojo";

                case 2:
                    return "verde";

                case 3:
                    return "naranja";

                case 4:
                    return "amarillo";

                case 5:
                    return "azul";
                case 6:
                    return "morado";

                default: 
                    return "";
                }
            } else {
                return "";
            }
        }

        private void clearPriorityStyle(){
            ObservableList<String> styleClasses = getStyleClass();
            this.getStyleClass().remove("rojo");
            styleClasses.remove("naranja");
            styleClasses.remove("verde");
            styleClasses.remove("amarillo");
            styleClasses.remove("azul");
            styleClasses.remove("morado");
        }
    };

and the .css code:

@CHARSET "UTF-8";
.amarillo {
    -fx-control-inner-background: khaki;
-fx-text-fill: blue;
-fx-background-insets: 0, 1, 2;
-fx-text-fill: -fx-selection-bar-text;
}

.errorRow {
    -fx-control-inner-background: green;

-fx-background-insets: 0, 1, 2;
-fx-text-fill: -fx-selection-bar-text;
}
.verde { 
  -fx-control-inner-background: palegreen;
  -fx-accent: derive(-fx-control-inner-background, -40%);
  -fx-cell-hover-color: derive(-fx-control-inner-background, -20%);
}

.azul { 
  -fx-control-inner-background: plum;
  -fx-accent: derive(-fx-control-inner-background, -40%);
  -fx-cell-hover-color: derive(-fx-control-inner-background, -20%);
}

.naranja { 
  -fx-control-inner-background: coral;
  -fx-accent: derive(-fx-control-inner-background, -40%);
  -fx-cell-hover-color: derive(-fx-control-inner-background, -20%);
}
.rojo { 
  -fx-control-inner-background: red;
  -fx-accent: derive(-fx-control-inner-background, -40%);
  -fx-cell-hover-color: derive(-fx-control-inner-background, -20%);
}
.morado { 
  -fx-control-inner-background: purple;
  -fx-accent: derive(-fx-control-inner-background, -40%);
  -fx-cell-hover-color: derive(-fx-control-inner-background, -20%);
}
.marron { 
  -fx-control-inner-background: brown;
  -fx-accent: derive(-fx-control-inner-background, -40%);
  -fx-cell-hover-color: derive(-fx-control-inner-background, -20%);
}

There are some bugs in your table row factory implementation:

  1. getStyleClass() returns a list, which can of course contain duplicate items. So you have to ensure that you are not adding the same item twice. The easiest way to do this is to remove all the colors from the style class list each time updateItem(...) is called, then just add back the one you need.
  2. You are not currently handling the empty case: here you want to make sure you remove any of the style classes you added.

You can fix these in one fell swoop. First note that while list.remove(item) removes the first occurrence of item from a list, list.removeAll(collection) will remove all elements in the list that belong to the supplied collection. Second, make sure you call clearPriorityStyle() even if the table row is empty.

Different versions of JavaFX may implement the table updates differently, so the likely reason you are seeing different behaviors is that one machine is using a version that exposes these bugs, while others are using a version whose implementation just happens to work with your code. Here's a fix:

public TableRow<Orden> call(TableView<Orden> tableView) {

    final TableRow<Orden> row = new TableRow<Orden>() {

        final List<String> allStyles = Arrays.asList(
            "", 
            "rojo", 
            "naranja", 
            "verde", 
            "amarillo", 
            "azul", 
            "morado");

        @Override
        protected void updateItem(Orden orden, boolean empty){
            super.updateItem(orden, empty);

            clearPriorityStyle();

            if(null!=orden) {
                String color = calcularColorRow(orden);
                if(!color.equals("")){
                    this.getStyleClass().add(color);

                }

            }

        }

        // same as before but a cleaner implementation:
        private String calcularColorRow(Orden o){
            if(null!=o.getmColorCode()){
                int colorCode = o.getmColorCode();
                if (colorCode < allStyles.size()) {
                    return allStyles.get(colorCode);
                }
            } 
            return "" ;
        }   


        private void clearPriorityStyle(){
            ObservableList<String> styleClasses = getStyleClass();
            this.getStyleClass().removeAll(allStyles);
        }

    };

    return row ;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM