Need to customize combobox in javafx with xml data

2020-04-20 12:18发布

问题:

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

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

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

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:

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

}