swing - JButton in single table cell java, not entire column -


i have been researching jtables thoroughly, , have found brilliant resources explain how set cellrenderer , celleditor columns, have found trying set renderer , editor of single cell little less forthcoming.

the general idea of table have 2 columns, left column displays variable names, , right column allows modification of associated variables. cells have been able perform validation on, , found out how use defaultcellrenderer , defaultcelleditor way required, trying custom renderers , editors struggling with. below have tried indicate how table under design look: (left hand value variable name, right hand value)

x 0

y 0

isdestructable [checkbox]

image [clickable cell]


the image value should allow user click cell, bring jfilechooser, , extract response , render string representation inside cell. have numerous snippets of code written, trying link them bit lost on. below have provided code have far relating these functions:

/* code creates table , passes custom model it. code setting other types of renderers , editors have got working have been omitted */ table = new jtable(new mydatamodel(columnnames, values)) {     public tablecellrenderer getcellrenderer(int row, int col)     {         if(col == 1)         {             if(table.getmodel().getvalueat(row, 0).equals("image"))             {                 //this want set renderer believe             }         }          return super.getcellrenderer(row, col);     }      public tablecelleditor getcelleditor(int row, int col)     {         if(col == 1)         {             if(table.getmodel().getvalueat(row, 0).equals("image"))             {                 //this want set editor believe             }          }           return super.getcelleditor(row, col);      } };  /* inner class, have created following editor should display jfilechooser when user clicks on cell */ private class buttontablecelleditor extends abstracttablecelleditor implements tablecelleditor {     private jframe mainframe;     private jfilechooser jfc;      public buttontablecelleditor(jframe frame)     {         mainframe = frame;         jfc = new jfilechooser();     }      @override     public void addcelleditorlistener(celleditorlistener arg0)     {      }      @override     public void cancelcellediting()     {         //hide jfilechooser here         super.cancelcellediting();     }      @override     public object getcelleditorvalue()     {         //here need return filename has been returned jfilechooser         return "test";     }      @override     public boolean iscelleditable(eventobject arg0)     {         return true;     }      @override     public void removecelleditorlistener(celleditorlistener arg0)     {         //not sure need use @     }      @override     public boolean shouldselectcell(eventobject arg0)     {         int returned = jfc.showopendialog(mainframe);          if(returned == jfilechooser.approve_option)         {             system.out.println("file fine");         }         else         {             stopcellediting();         }          return true;     }      public boolean stopcellediting()     {         return false;     }      public component gettablecelleditorcomponent(jtable table, object value, boolean arg2, int row, int col)     {          //this know incredibly important based off other examples,          //i unsure in here in case          return null;      } } 

all need (i think) getting buttontablecelleditor linked single cell require (condition if left hand column equals "image", , gettablecelleditorcomponent() in situation. hugely appreciated, have been stuck little while now.

also, first question (i have read numerous questions, , tried take note possible), if there feedback in way have posed question later questions may better structured, welcome. all!

based on answer provided in intercept jtable selection changeevents, , extremely useful direction madprogrammer listselectionlisteners, have been able come following solution:

table = new jtable(new custommodel(columnnames, values)) {     /*     have omitted other functions have overridden here not necessary     solution*/      @override     public void changeselection(int row, int col, boolean toggle, boolean extend)     {         if(col == 1 && table.getmodel().getvalueat(row, 0).equals("image"))         {             /*             imagechooser name of jfilechooser using, , passing             main frame of application centre window appears             */             int returned = imagechooser.showopendialog(frame);              if(returned == jfilechooser.approve_option)                 table.getmodel().setvalueat(imagechooser.getselectedfile().getname(), row, col);         }          super.changeselection(row, col, toggle, extend);     } } 

i not believe solution scalable, purpose of program, works extremely well. have been able open jfilechooser whenever appropriate cell clicked, , have string inside cell change represent name of file selected.

the cell acts button without being rendered one. depending on ui appearance requirements, may not appropriate ordinary table cell, potentially leading usability issues.

edit:

a more scalable approach determine type of value in column 1 "instanceof", apply objects of type.


Comments

Popular posts from this blog

python - mat is not a numerical tuple : openCV error -

c# - MSAA finds controls UI Automation doesn't -

wordpress - .htaccess: RewriteRule: bad flag delimiters -