java - JTable cell changes when clicking Button? -


i have table tablemodel containg names , cellrenderer extends panel made contains jbutton. everythign works great except when click on button values in cell disappear long hold jbutton clicked!? bellow panel , renderer. ideas?

        package gui.table;      import event.testcontroller;     import gui.daimlercolor;     import gui.mainwindow;      import java.awt.component;     import java.util.linkedlist;     import java.util.list;      import javax.swing.borderfactory;     import javax.swing.jtable;     import javax.swing.table.tablecellrenderer;      import org.jdom2.document;     import org.jdom2.element;       @suppresswarnings("serial")     public class testcasecellrenderer extends testcasepanel implements tablecellrenderer {          list<string> data;         mainwindow gui = null;         testcontroller tgc = null;          public testcasecellrenderer() {             super();             setname("table.cellrenderer");         }          public testcasecellrenderer(list<string> names, mainwindow gui2, testcontroller tgc) {             super();             this.data = names;             this.gui = gui2;             this.tgc = tgc;             setname("table.cellrenderer");         }          @override         public component gettablecellrenderercomponent(jtable table, object value,                 boolean isselected, boolean hasfocus, int row, int column) {              //this.setbackground(isselected ? table.getselectionbackground() : table             //      .getbackground());              this.setopaque(true);             this.settooltiptext("click play rerun testcase");              this.setname(data.get(row));              //set button image if passed             int passed = 0;              setplaybuttonicon(gui.gettestcasetablemodelicon(row));              setclickable(tgc.clickable);               if (row <= gui.currenttestcase) {                 document doc = tgc.getdoc();                 list<element> cases = doc.getrootelement().getchildren();                 list<element> teststeps = cases.get(row).getchildren();                 list<string> attributes = new linkedlist();                 (element teststep : teststeps) {                       if (teststep.getattribute("status") != null) {                         attributes.add(teststep.getattributevalue("status"));                     }                     else{                         attributes.add("empty");                     }                  }                 if(attributes.contains("empty")){                     setbutton("");                 }                 else if (attributes.contains("failed")){                      setbutton("failed");                 }                 else{                     setbutton("passed");                 }              } else {                 setbutton("");             }              this.setborder(borderfactory.createemptyborder());             this.setbackground(isselected ?  daimlercolor.light_blue : (row % 2 == 1 ? daimlercolor.dark_white : daimlercolor.white));             return this;         }       }        package gui.table; import gui.mainwindow;  import java.awt.gridbagconstraints; import java.awt.gridbaglayout; import java.awt.image; import java.awt.insets;  import javax.swing.imageicon; import javax.swing.jbutton; import javax.swing.jlabel; import javax.swing.jpanel;  @suppresswarnings("serial") public class testcasepanel extends jpanel {     public  jbutton playbutton = new jbutton();// new jbutton("play");     //     string name = "-";     jlabel namelabel = new jlabel(name);     jlabel statusiconlabel = new jlabel();     public mainwindow gui;      public void setname(string name) {         this.name = name;         namelabel.settext(name);     }      public void setclickable(boolean b){         playbutton.setenabled(b);     }      public void setplaybuttonicon(string s){         imageicon icon = new imageicon(system.getproperty("user.dir") + "\\ressources\\playbutton.png");         image img = icon.getimage();         image scaledimg = img.getscaledinstance(16, 16, java.awt.image.scale_smooth);         icon = new imageicon(scaledimg);           imageicon icon2 = new imageicon(system.getproperty("user.dir") + "\\ressources\\pausebutton.png");         image img2 = icon2.getimage();         image scaledimg2 = img2.getscaledinstance(16, 16, java.awt.image.scale_smooth);         icon2 = new imageicon(scaledimg2);          if(s.equals("play")){             playbutton.seticon(icon);         }else{             playbutton.seticon(icon2);         }     }      public void setbutton(string s) {          if (s.equals("passed")) {             imageicon icon = new imageicon(system.getproperty("user.dir") + "\\ressources\\check.png");             image img = icon.getimage();             icon = new imageicon(img);             statusiconlabel.seticon(icon);             statusiconlabel.setopaque(false);          } else if (s.equals("failed")) {             imageicon icon = new imageicon(system.getproperty("user.dir") + "\\ressources\\fail.png");             image img = icon.getimage();             icon = new imageicon(img);             statusiconlabel.seticon(icon);             statusiconlabel.setopaque(false);          } else {             imageicon icon = new imageicon(system.getproperty("user.dir") + "\\ressources\\help.png");             image img = icon.getimage();             icon = new imageicon(img);             statusiconlabel.seticon(icon);             statusiconlabel.setopaque(false);          }     }      public testcasepanel() {         super();         setopaque(true);          playbutton.setfocusable(false);         playbutton.setrolloverenabled(false);         gridbaglayout gbl = new gridbaglayout();         gridbagconstraints gbc = new gridbagconstraints();         this.setlayout(gbl);         gbc.gridx = 0;         gbc.gridy = 0;         gbc.weightx = 0;         gbc.insets = new insets(3, 3, 3, 3);         gbc.anchor = gridbagconstraints.west;         gbl.setconstraints(statusiconlabel, gbc);         this.add(statusiconlabel);          gbc.gridx = 1;         gbc.gridy = 0;         gbc.weightx = 0.8;         gbl.setconstraints(namelabel, gbc);         this.add(namelabel);          imageicon icon = new imageicon(system.getproperty("user.dir") + "\\ressources\\playbutton.png");         image img = icon.getimage();         image scaledimg = img.getscaledinstance(integer.valueof((int) this.getpreferredsize().getheight()).intvalue(),                 integer.valueof((int) this.getpreferredsize().getheight()).intvalue(), java.awt.image.scale_smooth);         icon = new imageicon(scaledimg);         playbutton.setname("play");         playbutton.seticon(icon);         gbc.gridx = 2;         gbc.gridy = 0;         gbc.weightx = 0;         gbl.setconstraints(playbutton, gbc);         this.add(playbutton);      }      public void setbuttonicon(string name){          if(name.equals("play")){             imageicon icon = new imageicon(system.getproperty("user.dir") + "\\ressources\\playbutton.png");             image img = icon.getimage();             image scaledimg = img.getscaledinstance(16,16, java.awt.image.scale_smooth);             icon = new imageicon(scaledimg);             playbutton.seticon(icon);           }else if (name.equals("pause")) {             imageicon icon = new imageicon(system.getproperty("user.dir") + "\\ressources\\pausebutton.png");             image img = icon.getimage();             image scaledimg =  img.getscaledinstance(16,16, java.awt.image.scale_smooth);             icon = new imageicon(scaledimg);             playbutton.seticon(icon);           }     }  } 

thanks ideas.

  • don't set/modify jcomponent, value inside xxxrenderer, don't change state of value (came xxxtablemodel)

  • xxxrenderer decorator not value generator,

  • for cell value (in xxxrenderer) resposible value stored in xxxtablemodel, set value in xxxtablemodel,

  • don't store jcomponents in xxxtablemodel, there stored rendering value


Comments