JavaFX Coloring TableCell

老子叫甜甜 提交于 2020-01-13 11:25:49

问题


I need your help!

I've got a Table with rows in it (Name, etc..) Now, I want to color a specific tableCells background when the Object, laying on this row has got a specific value. But i only get it to read the value of this cell. But i need to read the Object (in my code called TableListObject) to know in wich color i need to color the cell. But this "color value" is not visible (has no column) in that row.

Here is my code:

for(TableColumn tc:tView.getColumns()) {
    if(tc.getId().equals("text")) {
        tc.setCellValueFactory(newPropertyValueFactory<TableListObject,String>("text"));
        // here i need to check the Objects value and coloring that cell
    }
}

Here is a HTML Fiddle to visualize my problem: https://jsfiddle.net/02ho4p6e/


回答1:


Call the cell factory for the column you want and override the updateItem method. You need to check if it is empty and then if it is not you can do your object check and then you can set the color of the cell background or any other style you want. Hope this helps.

    tc.setCellFactory(column -> {
        return new TableCell<TableListObject, String>() {
            protected void updateItem(String item, boolean empty) {
                super.updateItem(item, empty);

                if (item == null || empty) {
                    setText(null);
                    setStyle("");
                } else {
                    if (item.equals("Something")) {
                        setStyle("-fx-background-color: blue");
                    } else {
                        setStyle("");
                    }
                }
            }
        };
    });

EDIT 1:

If you want to use the value of another cell in the same row. You will have to use the index of the row and get the items need for the check.

tc.setCellFactory(column - > {
   return new TableCell < TableListObject, String > () {
     protected void updateItem(String item, boolean empty) {
       super.updateItem(item, empty);

       if (item == null || empty) {
         setText(null);
         setStyle("");
       } else {
         int rowIndex = getTableRow().getIndex();
         String valueInSecondaryCell = getTableView().getItems().get(rowIndex).getMethod();
         if (valueInSecondaryCell.equals("Something Else")) {
           setStyle("-fx-background-color: yellow"); //Set the style in the first cell based on the value of the second cell
         } else {
           setStyle("");
         }

       }
     }
   };
 });

EDIT 2:

Improved answer based on suggestion. This uses the referenced object.

   else {
         TableListObject listObject = (TableListObject) getTableRow().getItem();
         if (listObject.getMethod().equals("Something Else")) {
           setStyle("-fx-background-color: yellow"); //Set the style in the first cell based on the value of the second cell
         } else {
           setStyle("");
         }
       }


来源:https://stackoverflow.com/questions/35056108/javafx-coloring-tablecell

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