ajax - JTable with annotation in Java -
how can create jtable in java using annotation? this: jtable table=new jtable(products.class). can i? products class several attributes like:id,price,productname.
there article , source code in codeproject this: http://www.codeproject.com/articles/36170/simple-and-powerfull-tablemodel-with-reflection
there no such annotations comes java runtime (at least now). can implement own method.
define annotation mark columns given object:
public @interface tablecolumn { string name(); int index(); }
mark fields want display in model object annotation:
class product { @tablecolumn(name="product name", index=0) string productname; .... }
create new tablemodel class -you can inherit defaulttablemodel- , check given annotation given object.
class objecttablemodel extends defaulttablemodel { private object[] objects; private string[] columns; public objecttablemodel(object[] objects) { field[] fields = object.class.getdeclaredfields(); (field field : fields) { if (field.isannotationpresent(tablecolumn.class)) { //add field list } } } @override public object getvalueat(int rowindex, int columnindex) { //return value objects[rowindex] } @override public int getrowcount() { return objects.length; } @override public int getcolumncount() { return columns.length; } @override public string getcolumnname(int column) { return columns[column]; } @override public class<?> getcolumnclass(int columnindex) { return columnclasses[columnindex]; } }
Comments
Post a Comment