Java Tips Weblog

  • Blog Stats

    • 2,076,564 hits
  • Categories

  • Archives

  • Advertisements

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)};
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

See Also

Row Table Model


7 Responses to “List Table Model”

  1. 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.


    • 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( ... );

  2. Dear Rob,

    your advice had been implemented, the result is correctly displayed.

    thank you for your kind excellent support.


  3. 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:

    ArrayList columnNames = new ArrayList();
    ArrayList data = new ArrayList();
    data = Data.getInstance().getProfessors();
    final ListTableModel extendedTableModel = new ListTableModel(data, columnNames);

    I tried different casting, but got nothing.
    Thanks in advance for your help! I really appreciate it.

  4. 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.

  5. 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 !

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: