如何根据单元格的值为 jtable 的单元格着色

How to color cells of jtable depending on cell's value

我想为 jtable 的某些特定单元格着色。这是我的渲染 class。我将 sysout 放在 if 块上。打印了所有字符串,但单元格的颜色除了其中一个没有改变。

public class MyRenderer extends DefaultTableCellRenderer {
    static double rpmMin, rpmMax, speedMin, speedMax, temperatureMin, temperatureMax, voltageMin, voltageMax;


  public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus,
        int row, int column) {
    Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
    if (!table.isRowSelected(row)) {
        if (column == 2 && Double.parseDouble(value.toString()) > rpmMin
                && Double.parseDouble(value.toString()) < rpmMax) {
            c.setBackground(Color.PINK);
        }
        if(column == 3 && Double.parseDouble(value.toString()) > speedMin
                && Double.parseDouble(value.toString()) < speedMax){
            c.setBackground(Color.PINK);
        }
        if (column == 4 && Double.parseDouble(value.toString()) > temperatureMin
                && Double.parseDouble(value.toString()) < temperatureMax) {
            c.setBackground(Color.PINK);
        }
        if(column == 5 && Double.parseDouble(value.toString()) > voltageMin
                && Double.parseDouble(value.toString()) < voltageMax){
            c.setBackground(Color.PINK);
        }
        else {
            c.setBackground(Color.GREEN);
        }
    }

    return c;
  }
}

Here is the output of my program. Only the first unsuitable value is colored pink.

我准备了一个 excel 来显示正确的输出。Here is the picture that I expected to see as output of this program

我不知道为什么它不起作用。有人可以向我解释一下吗?非常感谢:)

逻辑陷阱。您的个人 ifs 工作正常,只是您的最后一个 if 是一个 if/else 语句,它将把所有东西都变成绿色,除非它是适当的粉红色。

所以基本上,前 4 个 if 语句被忽略因为只有最后一个决定它是绿色还是粉红色。

另外,出于理智考虑,解析一次,重复使用两次或更多次。

    Double val = Double.parseDouble(value.toString());

    if (column == 2 && val > rpmMin
            && val < rpmMax) {
        c.setBackground(Color.PINK);
    }
    else if(column == 3 && val > speedMin
            && val < speedMax){
        c.setBackground(Color.PINK);
    }
    else if (column == 4 && val > temperatureMin
            && val < temperatureMax) {
        c.setBackground(Color.PINK);
    }
    else if(column == 5 && val > voltageMin
            && val < voltageMax){
        c.setBackground(Color.PINK);
    }
    else {
        c.setBackground(Color.GREEN);
    }

或者类似的东西(不是编译这个,请原谅粗糙):

int [][] minMaxes = { {0, 0},
                      {0, 0},
                      {rpmMin, rpmMax},
                      {speedMin, speedMax},
                      {temperatureMin, temperatureMax},
                      {voltageMin, voltageMax}
                    };
Color bgColor;
if (val > minMaxes[column][0] && val < minMaxes[column][1])
{
  bgColor = PINK;
}
else
{
  bgColor = GREEN;
}
c.setBackGround(bgColor);

对列等的值进行适当检查

编辑

为了防止日期和时间字符串的错误,我在你的代码中添加了一个 if 块,它运行良好。这是 getTableCellRendererComponent 方法。

public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus,
        int row, int column) {
    Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
    double [][] minMaxes = { {0, 0},
            {0, 0},
            {rpmMin, rpmMax},
            {speedMin, speedMax},
            {temperatureMin, temperatureMax},
            {voltageMin, voltageMax}
          };
    if (!table.isRowSelected(row)) {
        if(column == 0 || column == 1){
            c.setBackground(Color.WHITE);
        }
        else if (Double.parseDouble(value.toString())>minMaxes[column][0] && Double.parseDouble(value.toString())<minMaxes[column][1]) {
            c.setBackground(Color.PINK);
        }
        else {
            c.setBackground(Color.GREEN);
        }
    }

    return c;
}