简体   繁体   中英

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

I want to color some specific cells of a jtable. Here is my render class. I put sysout on the if blocks. All strings are printed but the color of cells didn't change except one of them.

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.

I prepared an excel to show the proper output. Here is the picture that I expected to see as output of this program

I don't know why it didn't work. Can somebody please explain it to me? Thanks a lot :)

Logic trap. Your individual ifs are working fine, it's just that your last if is an if/else statement which will turn everything green unless it's suitably pink.

So basically, the first 4 if statements are ignored as only the last one determines whether it is green or pink.

Also, for sanity purposes, parse once, reuse twice or more.

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

Or something like (not compiling this, please excuse roughness):

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

With appropriate checks on the value of column, etc.

EDIT

To prevent the errors cause of date and time string, I added one more if block to your code, and it worked perfectly. Here is the getTableCellRendererComponent method.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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