java - What is the best way to initialise a dynamic JTable with custom renderers? -
currently initialise jtable
in jpanel
so:
private arraydata data; private jtable table; private customtablemodel model; public extendedjpanel() { data = arraydata.getdata(); model = new customtablemodel(data); table = new jtable(model); tablecolumn col = table.getcolumnmodel().getcolumn(0); col.setcellrenderer(new column0renderer()); col = table.getcolumnmodel().getcolumn(1); col.setcellrenderer(new column1renderer()); // other stuff }
when want modify values in jtable
i interact tablemodel
directly creating new object[][]
array , calling firetablecellupdated(row, column)
cells.
my problem that, @ point in program, jtable
loses custom renderers.
i've been told because updating tablecolumnmodel
.
what's best way initialise jtable
such cellrenderers
stay constant throughout life of jtable
?
when want modify values in table, interact tablemodel directly creating new object[][] array , calling firetablecellupdated(row, column) cells.
no, should never call firetablecellupdated(...)
directly. job of tablemodel. changes data should done via tablemodel, not external data storage array.
if want change data in table use setvalueat(...) method of tablemodel.
you can add clear(...)
method custom tablemodel resets internal data.
what's best way initialise jtable such cellrenderers stay constant throughout life of jtable?
depends on mean initialize. when using defaulttablemodel can use:
model.setrowcount(0);
and data removed.
then can use addrows(...)
method dynamically add data.
my problem that, @ point in program, jtable loses custom renderers. i've been told because updating tablecolumnmodel.
yes, whenever like:
table.setmodel(...);
a new tablecolumnmodel created , lose custom renders. using suggestion above not problem, because haven't changed "structure" of table (just removed data), there no need recreate tablecolumnmodel.
Comments
Post a Comment