Swing development often encounters such a problem. You need to display the list of objects directly with jtable. At this time, we often need to rewrite the abstracttablemodel class to facilitate data display, however, writing is still cumbersome, So we further encapsulate the abstracttablemodel class to make it easier to use.
Import Java. util. arraylist; <br/> Import Java. util. list; </P> <p> Import javax. swing. table. abstracttablemodel; </P> <p> public abstract class myabstracttablemodel extends acttablemodel {<br/> Private Static final long serialversionuid = 1l; </P> <p> protected list <Object> datas = new arraylist <Object> (); <br/> protected class <?> [] Columns; <br/> protected string [] columnnames; </P> <p> protected abstract object getproperty (Object OBJ, int columnindex ); </P> <p> @ override <br/> Public int getcolumncount () {<br/> // todo auto-generated method stub <br/> return columns. length; <br/>}</P> <p> @ suppresswarnings ("unchecked") <br/> Public void setdata (list datas) {<br/> This. datas = datas; <br/>}</P> <p> public list <Object> List () {<br/> retu Rn datas; <br/>}</P> <p> Public object GetObject (INT rowindex) {<br/> return datas. get (rowindex); <br/>}</P> <p> @ override <br/> Public int getrowcount () {<br/> // todo auto-generated method stub <br/> return datas. size (); <br/>}</P> <p> protected class <?> Getfieldtype (class <?> Clz, string field) {<br/> try {<br/> class <?> Result = clz. getdeclaredfield (field ). getType (); <br/> If (result. equals (Int. class) {<br/> result = integer. class; <br/>} else if (result. equals (Boolean. class) {<br/> result = Boolean. class; <br/>} else if (result. equals (byte. class) {<br/> result = byte. class; <br/>} else if (result. equals (char. class) {<br/> result = character. class; <br/>} else if (result. equals (Long. class) {<br/> result = lon G. class; <br/>} else if (result. equals (float. class) {<br/> result = float. class; <br/>} else if (result. equals (double. class) {<br/> result = double. class; <br/>}< br/> return result; <br/>} catch (securityexception E) {<br/> // todo auto-generated Catch Block <br/> E. printstacktrace (); <br/>} catch (nosuchfieldexception e) {<br/> // todo auto-generated Catch Block <br/> E. printstacktrace (); <br/>}< Br/> return string. Class; <br/>}</P> <p> @ override <br/> public class <?> Getcolumnclass (INT columnindex) {<br/> // todo auto-generated method stub <br/> If (columnindex <0 | columnindex> columns. length) <br/> return NULL; <br/> return columns [columnindex]; <br/>}</P> <p> @ override <br/> Public Boolean iscelleditable (INT rowindex, int columnindex) {<br/> // todo auto-generated method stub <br/> return false; <br/>}</P> <p> @ override <br/> Public String getcolumnname (INT column) {<br/> // todo auto-generated method stub <br/> If (column <0 | column> columns. length) <br/> return NULL; <br/> return columnnames [column]; <br/>}</P> <p> @ override <br/> Public object getvalueat (INT rowindex, int columnindex) {<br/> If (rowindex> getrowcount () | columnindex> getcolumncount () <br/> | rowindex <0 | columnindex <0) <br/> return NULL; <br/> return getproperty (datas. get (rowindex), columnindex); <br/>}</P> <p>}
In this case, you only need to implement the myabstracttablemodel class and override the getproperty method. Of course, we will add another constructor for convenience, see the following implementation (where the workorder class is a pojo class)
Public class workordertablemodel extends deltatablemodel {<br/> Private Static final long serialversionuid = 1l; </P> <p> protected list <workorder> datas = new arraylist <workorder> (); </P> <p> Public workordertablemodel (list <workorder> datas, string [] columnnames) {<br/> This. datas = (datas = NULL )? New arraylist <workorder> (): datas; <br/> This. columnnames = columnnames; <br/> Columns = new class [] {<br/> getfieldtype (workorder. class, "Number"), <br/> getfieldtype (workorder. class, "modelname"), <br/> // getfieldtype (workorder. class, "createdate"), <br/> getfieldtype (workorder. class, "targetqty"), <br/> getfieldtype (workorder. class, "routecode"), <br/> getfieldtype (workorder. class, "owner") };< br/>}</P> <p> @ override <br/> protected object getproperty (Object OBJ, int index) {<br/> workorder = (workorder) OBJ; <br/> Object result = ""; <br/> switch (INDEX) {<br/> case 0: <br/> result = workorder. getnumber (); <br/> break; <br/> case 1: <br/> result = workorder. getmodelname (); <br/> break; <br/> // Case 2: <br/> // result = workorder. getcreatedate (); <br/> // break; <br/> case 2: <br/> result = workorder. gettargetqty (); <br/> break; <br/> case 3: <br/> result = workorder. getroutecode (); <br/> break; <br/> case 4: <br/> result = workorder. getowner (); <br/> break; <br/>}< br/> return result; <br/>}< br/>}