1
votes

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.

1
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 ifs 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
Note: statements of the form xyzProperty().getValue() can be usually writen as getXyz() (or isXyz() for BooleanPropertys) which I personally prefer for being more concise. - fabian

1 Answers

0
votes

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

}