java - Netbeans, creating custom table model from drag and drop gui designer. -
i have application use inventory of items, trying add jtable jframe in code. have created extension of abstracttablemodel
object called itemtablemodel
. have dragged , dropped jtable onto designer view in netbeans respective jframe, , renamed m_jttable
.
public class itemtablemodel extends abstracttablemodel { inventory inv; int rowcount = 0; int colcount = 3; public itemtablemodel(inventory in) { inv = in; } @override public int getrowcount() { return inv.itemlist.size(); } @override public int getcolumncount() { return colcount; } @override public boolean iscelleditable(int row, int col) { return false; } @override public object getvalueat(int rowindex, int columnindex) { item = inv.itemslist.get(rowindex); object reto = null; switch(columnindex) { case 0: reto = (object)it.getitemnumber(); break; case 1: reto = (object)it.getdescription(); break; case 2: reto = (object)it.getprice(); break; default: reto = (object)new string("null"); break; } return reto; } }
then in method in jframe have code below after inventory loaded
itemtablemodel data = new itemtablemodel(this.inv); m_jttable.setmodel(data);
i have few questions, need call draw table? need specify table model netbeans specifically? how specify column names? , if changes in inventory, how redraw table reflect this? (i plan search inventory , display possible results in table changing inventory
1 search matches)
also, don't need/want make cell editable, table viewing.
what need call draw table?
generally, nothing. if table has being added visible component, setting model automatically update table contents of model
do need specify table model netbeans specifically?
while can "build" model in editor, generally, no. should simple create model , apply table within code source, after initcomponent
has being called...
how specify column names?
there couple of ways, easiest override tablemodel#getcolumnname
method , return appropriate name requested index
and if changes in inventory, how redraw table reflect this?
this depend on how model implemented. recreate model , re-apply table.
a better solution provide add
method, allow add new inventory item
s table, raise appropriate event using [abstracttablemodel#firetablerowsinserted
](http://docs.oracle.com/javase/7/docs/api/javax/swing/table/abstracttablemodel.html#firetablerowsinserted(int, int)) cause table update self.
if want update existing item, can either use setvalueat
method directory, or provide means can tell model given item has begin changed, example itemchanged(item item)
need find row item
represents , fire [abstracttablemodel#firetablecellupdated
](http://docs.oracle.com/javase/7/docs/api/javax/swing/table/abstracttablemodel.html#firetablecellupdated(int, int))
(i plan search inventory , display possible results in table changing inventory 1 search matches)
take @ how use tables, sorting , filtering
also, don't need/want make cell editable, table viewing.
you've overridden iscelleditable
return
false
, should working...
take time read through how use tables lots more details
Comments
Post a Comment