Reputation: 382
I have been wondering why a jTable hasn't got an .addRow()
method by default. Why do you have to set a Model
before this is possible?
JTable table = new JTable();
table.addRow();
The above is not possible, however:
JTable table2 = new JTable();
table2.setModel(new DefaultTableModel());
table2.addRow(...);
After setting the new model, it IS possible - why?
Upvotes: 0
Views: 950
Reputation: 347194
First of all, by default, the TableModel
is not mutable (other then being able to, potentially, modify the existing data), that is, there are no methods within TableModel
that provide any means to add or delete rows.
It is up to implementations of TableModel
to decide if that functionality is possible. Take a look at TableModel
for details about what the default interface provides
Secondly, it is the responsibility of the model to manage the data. It makes no sense for the table to suddenly provide add/delete functionality, when that functionality may or may not exist. Modifications to the data should be done directly via the model - IMHO
Thirdly, there is no JTable#addRow
method
Upvotes: 2