简体   繁体   English

JavaFX Coloring TableCell

[英]JavaFX Coloring TableCell

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. 我有一个带有行的表(名称等等)。现在,我想在对象上放置一个特定的tableCells背景,在这一行上有一个特定的值。 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. 但我需要读取Object(在我的代码中称为TableListObject )以了解我需要为单元格着色的颜色。 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/ 这是一个可视化我的问题的HTML小提琴: https//jsfiddle.net/02ho4p6e/

Call the cell factory for the column you want and override the updateItem method. 在单元工厂中调用所需的列并覆盖updateItem方法。 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: 编辑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: 编辑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("");
         }
       }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM