Click to See Complete Forum and Search --> : JTable and TableModel


Colin
04-27-2000, 12:42 AM
JTable table = new JTable(model);
the line is to make a reference from table to model.
The data is stored in the model.
How to implement the reference between the data and model?

Paul Clapham
04-27-2000, 11:13 AM
Colin <Colin@hotmail.com> wrote in message news:3907c54b$1@news.devx.com...
>
> JTable table = new JTable(model);
> the line is to make a reference from table to model.
> The data is stored in the model.
> How to implement the reference between the data and model?
>
Your model has to be able to tell the JTable what's in the data. So you
have to implement a TableModel (I think) interface or extend
AbstractTableModel (that's what I did). Then you have to provide methods
getRowCount(), getColumnCount(), getValueAt(), and setValueAt() plus
possibly some others (e.g. getColumnName(), isCellEditable()). I learned
all this from a very good article on Sun's developer site, but I can't track
it down just now (I only spent 30 seconds looking).

This doesn't mean that your TableModel object has to actually contain the
data. It just has to be able to provide the data when getValueAt() is
called and to update it when setValueAt() is called.