JavaFX着色TableCell [英] JavaFX Coloring TableCell

查看:174
本文介绍了JavaFX着色TableCell的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要你的帮助!

我有一个带有行的表(名称等). 现在,当放置在此行上的对象具有特定值时,我想为特定的tableCells背景着色.但是我只能读取它的值.但是我需要读取对象(在我的代码中称为TableListObject),以非常清楚的颜色知道我需要给单元着色.但是该颜色值" 在该行中不可见(无列).

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.

这是我的代码:

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
    }
}

这是一个HTML小提琴,用于可视化我的问题: https://jsfiddle.net/02ho4p6e/

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

推荐答案

为所需的列调用单元格工厂,并覆盖updateItem方法.您需要检查它是否为空,如果不是,则可以进行对象检查,然后可以设置单元格背景的颜色或所需的任何其他样式.希望这会有所帮助.

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("");
                    }
                }
            }
        };
    });

如果要使用同一行中另一个单元格的值.您将必须使用行的索引并获取检查所需的项目.

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("");
         }

       }
     }
   };
 });

基于建议的改进答案.这将使用引用的对象.

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("");
         }
       }

这篇关于JavaFX着色TableCell的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆