Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -


i rendering jbutton image on simulate jcheckbox behavior. why don't let jtable render jcheckbox? because need bigger checkboxes , there's no way can resize them (as far know).

so, have own table cell renderer , editor. both renders jbutton , checkbox images on them. trying change image checked image unchecked image (or vice versa) , update cell value (boolean true or false). however, reason, getcelleditorvalue() not update table. so, release mouse, value goes original one.

any appreciated! lot!

    public class tableexample extends jframe {      jscrollpane pane;     jpanel panel;     jtable table;     object[][] data;     mytablemodel tm;     renderer buttoncolumn;      public tableexample(object[][] data) throws ioexception {          string[] columns = {"column#1", "column#2", "column#3", "column#4", "column#5", "column#6"};         this.data = data;         this.setpreferredsize(new dimension(700, 500));         this.setminimumsize(new dimension(700, 500));         this.setmaximumsize(new dimension(700, 500));         tm = new mytablemodel(this.data, columns);         this.table = new jtable(tm);         this.table.setrowheight(50);         table.setdefaultrenderer(boolean.class, new renderer(this, table, 5));         table.setdefaulteditor(boolean.class, new editor(this, table, 5));         this.pane = new jscrollpane(this.table);         this.add(this.pane);      }      /**      *      * table model      */     public class mytablemodel extends abstracttablemodel {          private object[][] data;         private string[] columns;          public mytablemodel(object[][] data, string[] columns) {             this.data = data;             this.columns = columns;          }          public void setcolumns(string[] columns1) {             this.columns = columns1;         }          @override         public int getrowcount() {             return this.data.length;         }          @override         public int getcolumncount() {             return this.columns.length;         }          @override         public string getcolumnname(int columnindex) {             return columns[columnindex];         }          @override         public class<?> getcolumnclass(int c) {             return getvalueat(0, c).getclass();         }          @override         public boolean iscelleditable(int rowindex, int columnindex) {             return true;         }          @override         public object getvalueat(int rowindex, int columnindex) {             return data[rowindex][columnindex];         }          public void setvalueat(string value, int row, int col) {              if (debug) {                 system.out.println("setting value @ " + row + "," + col                         + " " + value                         + " (an instance of "                         + value.getclass() + ")");             }              data[row][col] = value;             firetablecellupdated(row, col);              if (debug) {                 system.out.println("new value of data:");                 printdebugdata();             }         }          private void printdebugdata() {             int numrows = getrowcount();             int numcols = getcolumncount();              (int = 0; < numrows; i++) {                 system.out.print("    row " + + ":");                 (int j = 0; j < numcols; j++) {                     system.out.print("  " + data[i][j]);                 }                 system.out.println();             }             system.out.println("--------------------------");         }      }     /**      *      * cell renderer      */      private class renderer extends jbutton implements tablecellrenderer {          imageicon check;         imageicon uncheck;         boolean ischecked;          public renderer(jframe frame, jtable table, int column) throws ioexception {              image checki = imageio.read(getclass().getresource("ukbuttonblue.png"));             image unchecki = imageio.read(getclass().getresource("ukbuttonrev.png"));             this.setborderpainted(false);             check = new imageicon(checki);             uncheck = new imageicon(unchecki);          }          @override         public component gettablecellrenderercomponent(                 jtable table, object value, boolean isselected, boolean hasfocus, int row, int column) {             this.ischecked = (boolean) value;             if (ischecked) {                 seticon(check);              } else {                 seticon(uncheck);              }              return this;         }     }      /**      *      * cell editor      */      private class editor extends abstractcelleditor implements tablecelleditor, actionlistener {          private jbutton editbutton;          imageicon check;         imageicon uncheck;         boolean ischecked;          public editor(jframe frame, jtable table, int column) throws ioexception {              editbutton = new jbutton();             editbutton.addactionlistener(this);             editbutton.setborderpainted(false);              image checki = imageio.read(getclass().getresource("check.png"));             image unchecki = imageio.read(getclass().getresource("uncheck.png"));             check = new imageicon(checki);             uncheck = new imageicon(unchecki);          }          @override         public component gettablecelleditorcomponent(                 jtable table, object value, boolean isselected, int row, int column) {              this.ischecked = (boolean) value;             if (this.ischecked) {                 editbutton.seticon(uncheck);             } else {                 editbutton.seticon(check);             }             system.out.println("edit " + ischecked);              return editbutton;          }          @override         public object getcelleditorvalue() {              system.out.println("giden " + ischecked);             return this.ischecked;          }          @override         public void actionperformed(actionevent e) {              if (this.ischecked) {                 this.ischecked = false;             } else {                 this.ischecked = true;             }              fireeditingstopped();          }     }      public static void main(string[] args) throws ioexception {          object[][] data = {             {"1", "allan", "smith", false, true, false},             {"2", "jerry", "tucker", true, false, true}         };          tableexample app = new tableexample(data);         app.setvisible(true);      } } 

after working on while realized mytablemodel's iscelleditable method not working properly. switched detafulttablemodel overriding column class.

    @override     public class<?> getcolumnclass(int c) {         return getvalueat(0, c).getclass();     } 

one problem using defaulttablemodel boolean class rendered regular jcheckboxes automatically. so, instead of using boolean class, used integer class simulate true , false 0 , 1.

here final program simulate jcheckbox behavior in jtable using button , image. gives great flexibility rendering different images , still jcheckbox behavior. so, enjoy!

    public class tableexample extends jframe {      jscrollpane pane;     jpanel panel;     jtable table;     object[][] data;     defaulttablemodel tm;     renderer buttoncolumn;      public tableexample(object[][] data) throws ioexception {          string[] columns = {"column#1", "column#2", "column#3", "column#4", "column#5", "column#6"};         this.data = data;         this.setpreferredsize(new dimension(700, 500));         this.setminimumsize(new dimension(700, 500));         this.setmaximumsize(new dimension(700, 500));         //tm = new defaulttablemodel(this.data, columns);         defaulttablemodel tm = new defaulttablemodel(this.data, columns) {             @override             public class<?> getcolumnclass(int c) {                  return getvalueat(0, c).getclass();             }         };          this.table = new jtable(tm);         this.table.setrowheight(50);         table.setdefaultrenderer(integer.class, new renderer(this, table, 5));         table.setdefaulteditor(integer.class, new editor(this, table, 5));         this.pane = new jscrollpane(this.table);         this.add(this.pane);      }      /**      *      * renderer      */     private class renderer extends jbutton implements tablecellrenderer {          imageicon check;         imageicon uncheck;         int ischecked;          public renderer(jframe frame, jtable table, int column) throws ioexception {              image checki = imageio.read(getclass().getresource("check.png"));             image unchecki = imageio.read(getclass().getresource("uncheck.png"));             this.setborderpainted(false);             check = new imageicon(checki);             uncheck = new imageicon(unchecki);          }          @override         public component gettablecellrenderercomponent(                 jtable table, object value, boolean isselected, boolean hasfocus, int row, int column) {             this.ischecked = (int) value;             if (ischecked == 1) {                 seticon(check);              } else {                 seticon(uncheck);              }              return this;         }     }     /**      *      * editor      */      private class editor extends abstractcelleditor implements tablecelleditor, actionlistener {          private jbutton editbutton;          imageicon check;         imageicon uncheck;         int ischecked;          public editor(jframe frame, jtable table, int column) throws ioexception {              editbutton = new jbutton();             editbutton.addactionlistener(this);             editbutton.setborderpainted(false);              image checki = imageio.read(getclass().getresource("check.png"));             image unchecki = imageio.read(getclass().getresource("uncheck.png"));             check = new imageicon(checki);             uncheck = new imageicon(unchecki);          }          @override         public component gettablecelleditorcomponent(                 jtable table, object value, boolean isselected, int row, int column) {              this.ischecked = (int) value;             if (this.ischecked == 1) {                 editbutton.seticon(uncheck);                 this.ischecked = 0;             } else {                 editbutton.seticon(check);                 this.ischecked = 1;             }              return editbutton;          }          @override         public object getcelleditorvalue() {              return this.ischecked;          }          @override         public void actionperformed(actionevent e) {              fireeditingstopped();          }          public void updatevalue() {          }     }      public static void main(string[] args) throws ioexception {          object[][] data = {             {"1", "allan", "smith", false, false, 0},             {"2", "jerry", "tucker", true, true, 1}         };          tableexample app = new tableexample(data);         app.setvisible(true);      }  } 

Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -