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


i want color specific cells of jtable. here render class. put sysout on if blocks. strings printed color of cells didn't change except 1 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 output of program. first unsuitable value colored pink.

i prepared excel show proper output.here picture expected see output of program

i don't know why didn't work. can please explain me? lot :)

logic trap. individual ifs working fine, it's last if if/else statement turn green unless it's suitably pink.

so basically, the first 4 if statements ignored last 1 determines whether green or pink.

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

Comments

Popular posts from this blog

jOOQ update returning clause with Oracle -

java - Warning equals/hashCode on @Data annotation lombok with inheritance -

java - BasicPathUsageException: Cannot join to attribute of basic type -