1

I need to display the javafx tree table with values read from XML file. I am able to do it as shown below,

Tree Table view

I am able to add color to the combo box as shown

Combobox with color codes

But when I collapse the tree the color which is set still remains the same, as shown here

Error in combo box

How can I change it back to normal?

This is the piece of code I tried for changing color to the combobox where dojColumn is a column to display "Status"

dojColumn.setCellFactory(new Callback<TreeTableColumn<TestSet, String>, TreeTableCell<TestSet, String>>(){

    @Override
    public TreeTableCell<TestSet, String> call(TreeTableColumn<TestSet, String> param) {
        // TODO Auto-generated method stub

        return new ComboBoxTreeTableCell<TestSet,String>(list){

            public void updateItem(String status,boolean empty) {
                super.updateItem(status, empty);
                 int currentIndex = indexProperty().getValue() < 0 ? 0: indexProperty().getValue();
                String clmStatus = dojColumn.getCellData(currentIndex);
                if(!empty) {
                    if (status == null || empty) {
                        setStyle("");
                    }
                    else if (clmStatus.equals("Passed")) {
                        setTextFill(Color.BLACK);
                        //setStyle("-fx-font-weight: bold");
                        setStyle("-fx-background-color: green");
                        setText(clmStatus);

                    } else if (clmStatus.equals("Failed")){
                        setTextFill(Color.BLACK);
                        //setStyle("-fx-font-weight: bold");
                        setStyle("-fx-background-color: red");
                       setText(clmStatus);
                    } else if (clmStatus.equals("NotRelevant")){
                        setTextFill(Color.BLACK);
                       // setStyle("-fx-font-weight: bold");
                        setStyle("-fx-background-color: blue");
                      setText(clmStatus);
                    } 
                }
            }
        };
    }

});

can anyone help me with this. Thanks in advance.

fabian
  • 80,457
  • 12
  • 86
  • 114
  • Imho the question is now in a state where it can be reopened. Votes of 2(?) other people are needed for this though. Seems like a problem similar to others though: You are not dealing with the state change of a cell with an item to an empty cell properly (if `empty` is `false` so should be the condition of the first inner `if`). Furthermore doing the index logic outside of the `if`s and dealing with empty cells with a tenary expression there seems weird. Just put it at a point where you know the cells is non-empty. Furthermore clmStatus shouldn't be different from status afaik. – fabian Jan 03 '20 at 09:27
  • Note: statements of the form `xyzProperty().getValue()` can be usually writen as `getXyz()` (or `isXyz()` for `BooleanProperty`s) which I personally prefer for being more concise. – fabian Jan 03 '20 at 09:30

1 Answers1

0

Your issue seems to be in your update item.

You are checking that it's not empty, then if empty set back to ""...

if(!empty) {
    if (status == null || empty) {
         setStyle("");
    }
    etc...

Just remove the outer check - you don't the our if(!empty){}

Just use this:

if (status == null || empty) {
   setStyle("");
}
else if (clmStatus.equals("Passed")) {
    setTextFill(Color.BLACK);
    setStyle("-fx-background-color: green");
    setText(clmStatus);
} else if (clmStatus.equals("Failed")){
    setTextFill(Color.BLACK);
    setStyle("-fx-background-color: red");
    setText(clmStatus);
} else if (clmStatus.equals("NotRelevant")){
    setTextFill(Color.BLACK);
    setStyle("-fx-background-color: blue");
    setText(clmStatus);

}

purring pigeon
  • 4,141
  • 5
  • 35
  • 68