List Table Model
Posted by Rob Camick on November 24, 2008
The ListTableModel is a concrete implementation of the RowTableModel introduced in a previous entry. It may provide a more flexible solution, than can be achieved by using the DefaultTableModel, because of the increased functionality inherited from the RowTableModel.
First, it is called a ListTableModel because each row in the TableModel is represented by a List Object. Therefore, it is backwards compatible with old data because you can add rows of Vectors or it is compatible with the new Collections API as you can add rows of ArrayLists, for example.
Two additional methods have been added to support Arrays.
- insertRow – insert an Array of Objects at the specified row
- insertRows – insert a 2 dimensional array of Objects at the specified row
In both cases the data in the Array will be copied to a List and the List will be inserted into the model. Because data is simply copied from an Array to a List, it it better to use a List from the beginning when possible.
There is not much more to say about the ListTableModel. You would use it the same way you would use a DefaultTableModel. If you create a simple model like this…
String[] cn = {"First Name", "Last Name", "Age"};
ListTableModel model = new ListTableModel(Arrays.asList(cn));
model.setColumnClass(2, Integer.class);
model.setColumnEditable(2, false);
Object[] r1 = {"Homer", "Simpson", new Integer(40)};
Object[] r2 = {"Marge", "Simpson", new Integer(35)};
model.addRow(r1);
model.addRow(r2);
JTable table = new JTable(model);
…then you will get a table that looks something like this:
Note how you are able to set the column class and editable properties without creating a custom TableModel. This is not possible when using the DefaultTableModel.
Get The Code
ListTableModel.java
RowTableModel.java
andrifirstianto said
Dear Rob,
thank you for your excellent class it solved my requirement.
But another issue come out, after get the resultset ALL COLUMNS displayed at JTable.
how to display just selected ColumnNames.
ALL columns consists of Name, Sex, Age, Married, Address, Post Code, Countries, DOB.
while i just want to display selected columns only (e.g 3 columsn) Name, SEX and countries.
thank you for your kind attention and help.
respectfully,
andri
Rob Camick said
The best way is to change the SQL query to only return the columns that you want dislayed.
Otherwise you would need to use:
table.getColumnModel().removeColumn( ... );
andrifirstianto said
Dear Rob,
your advice had been implemented, the result is correctly displayed.
thank you for your kind excellent support.
respectfully,
andri
Vladimir said
Hello, Rob,
could you please help me with the ListTableModel initialization? Well we have a constructor like this:
ListTableModel(List modelData, List columnNames).
So we can initialize this table model with ArrayList of custom objects and the arraylist of strings (column names), but it the following code return doesn’t work for some reason:
I tried different casting, but got nothing.
Thanks in advance for your help! I really appreciate it.
Rob Camick said
You can’t use the LIstModel with “custom Objects” directly. Instead you need to use the BeanTableModel or an extension of the BeanTableModel. See my next posting for the code and an example.
christine said
Hi Rob,
I have a list of strings (song with a title and artist) I would like to display in a table. This list is contained in a class ‘Songs’ in a model package. How do I call this list to display in this table?
Thankyou so much for your help !
Rob Camick said
Since you have a custom class, Songs, you should be using the BeanTableModel. Check out that blog entry for more information and an example.