Need to customize combobox in javafx with xml data

☆樱花仙子☆ 提交于 2020-01-11 11:22:15

问题


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

}



来源:https://stackoverflow.com/questions/59560370/need-to-customize-combobox-in-javafx-with-xml-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!