Dashboard > Spring Rich Client Project (Spring Rich) > User Documentation > Table Support
  Spring Rich Client Project (Spring Rich) Log In View a printable version of the current page.  
  Table Support
Added by Peter De Bruycker, last edited by Peter De Bruycker on Nov 16, 2004  (view change)
Labels: 
(None)

Table Support

Spring RCP provides utility and convenience classes for working with tables.

Table Models

When using a table, you can use any implementation of TableModel, but two convenience classes are provided by the rcp project, BeanTableModel and ListTableModel.

When using one of these two classes, an extra column is added to the table, showing the row indexes. To toggle these, use the model.setRowNumbers(boolean) method.

To control the editable state of the cells in the table, override the isCellEditableInternal method.

ListTableModel model = new ListTableModel(rows)
{
  ... // other methods ommitted

  public boolean isCellEditableInternal(int rowIndex, int columnIndex) {
    // the second column is editable
    return columnIndex == 1;
  }
};

BeanTableModel

The BeanTableModel is used to display a List of beans in a table. A BeanTableModel needs the bean Class, a List of beans and a MessageSource.

Example
The Customer bean:

Customer.java
public class Customer {
  private String name;
  private String number;

  public String getName() {
    return name;
  }
  public void setName(String name) {
    this.name = name;
  }

  public String getNumber() {
    return name;
  }
  public void setNumber(String number) {
    this.number = number;
  }
}

The BeanTableModel implementation. Two methods need to be overrriden, createColumnPropertyNames and createColumnClasses.

The createColumnPropertyNames method returns an array of attribute names (i.e. number and name), and the createColumnClasses method returns an array of the attribute types.

CustomerTableModel.java
public class CustomerTableModel extends BeanTableModel {

    public CustomerTableModel(MessageSource messageSource) {
        super(Customer.class, messageSource);
    }

    protected String[] createColumnPropertyNames() {
        return new String[] { "number", "name" };
    }

    protected Class[] createColumnClasses() {
        return new Class[] { String.class, String.class };
    }
}

The view implementation

CustomerTableView.java
protected JComponent createControl() {
    // fetch the customer store
    CustomerStore store = ...;

    // fetch the messageSource instance from the application context 
    MessageSource messageSource = (MessageSource) getApplicationContext()
            .getBean("messageSource");

    // create the CustomerTableModel
    BeanTableModel tableModel = new CustomerTableModel(messageSource);
    tableModel.setRows(store.getCustomers());

    // create the JTable instance
    JTable table = TableUtils.createStandardSortableTable(tableModel);
    return new JScrollPane(table);
}

The messages.properties file contains the labels used for the column headers of the table.

customer.number=Number
customer.name=Name

ListTableModel

With a ListTableModel you can create a TableModel from a List of Objects, Lists or arrays.

It works as follows:

  • more than one column
    • rows are arrays: the column indexes correspond with the array indexes
    • rows are lists: the column indexes correspond with the list indexes
    • rows are objects: an IllegalArgumentException will be thrown
  • one column
    • rows are arrays: the value at index 0 of the array is used
    • rows are objects: the object itself is used

Example

You have a list of arrays you want to show:

List rows = new ArrayList();
rows.add(Arrays.asList("000001", "Apple"));
rows.add(Arrays.asList("000002", "Sun"));
rows.add(Arrays.asList("000002", "IBM"));

ListTableModel model = new ListTableModel(rows)
{
  protected Class[] createColumnClasses() {
    return new Class[] { String.class, String.class };
  }

  protected String[] createColumnNames() {
    return new String[] { "Number", "Name" };
  }
};

Result:

Number Name
000001 Apple
000002 Sun
000003 IBM

Example

You want to show a list of objects in a table with one column.

List rows = new ArrayList();
rows.add("Apples");
rows.add("Bananas");
rows.add("Kiwis");

ListTableModel model = new ListTableModel(rows)
{
  protected Class[] createColumnClasses() {
    return new Class[] { String.class };
  }

  protected String[] createColumnNames() {
    return new String[] { "Fruit" };
  }
};

Result:

Fruit
Apples
Bananas
Kiwis

Table Rendering

Several default CellRenderers are installed when you create a table with the TableUtils class.

  • Objects: BeanTableCellRenderer
  • CodedEnums: CodedEnumTableCellRenderer
  • Date: DateTimeTableCellRenderer
  • Boolean: BooleanTableCellRenderer()

To install your own renderer or to override a default renderer: do the following:

JTable table = TableUtils.createStandardSortableTable(model);
table.setDefaultRenderer(Date.class, new DateTimeTableCellRenderer(new SimpleDateFormat("dd/MM/yyyy")));

TODO: write your own renderer class

Editable Tables

TODO

Sortable Tables

You can create a sortable table by using the TableUtils.createStandardSortableTable method. All columns will be sortable. If a column class doesn't implement the Comparable interface, the toString implementation of the value will be used for comparison.

A SortableTableModel will wrap your own model, handling the sorting. Your own tablemodel will never change when the user sorts a column!

You will have to take care of the conversion of rowIndexes of the table to the rowIndexes of your model.

You can use the getSelectedRow(JTable) and getSelectedRows(JTable) of the TableUtils utility class.

public class TestTableView extends AbstractView {
    private ListTableModel model;
    private JTable table;

    protected JComponent createControl() {
        model = ...       
        table = TableUtils.createStandardSortableTable(model);

        return new JScrollPane(table);
    }
    
    private Object getSelectedRow()
    {
        int rowIndex = TableUtils.getSelectedRow(table);
        return model.getRow(rowIndex);
    }
}

Table Sort Indicator

TODO

Large Table Support

TODO

Table Updater

TODO

Tips and Tricks

Minimize the amount of grey on the screen.

To minimize the amount of grey on your views, you can set the background of the scrollpane containing the table to white.

JTable table = ...
JScrollPane scrollPane = new JScrollPane(table);
scrollPane.getViewport().setOpaque(true);
scrollPane.getViewport().setBackground(table.getBackground());

Grey background

White background

Site running on a free Atlassian Confluence Open Source Project License granted to Spring Framework. Evaluate Confluence today.
Powered by Atlassian Confluence, the Enterprise Wiki. (Version: 2.5.5 Build:#811 Jul 25, 2007) - Bug/feature request - Contact Administrators