Minimizing TableModel maintenance


Implementing TableModel in your Swing application is relatively simple.  One of the straight-forward ways to do it was always to use switch statements to return or save data according to row/column number. And this is were the problem is. Every time you need to change default sequence of your columns you have to touch all methods with the switch statements.

The solution I came up with is very simple and is based on Java enums.

Define your columns as enum inside of your table model. This will be the default sequence of your columns. All you have to do now is to convert from column index to enum according to the sequence of enums. After that you can implement your switch statements using enums instead of column index. Going one step farther we can convert enum to column title. This brings us to an idea of class to support such table model implementation. I called it TableColumDefinitionSupport. Here it is.


public final class TableColumnDefinitionSupport {

	public interface ITableColumDefinition {
		String getTitle();
	}

	private TableColumnDefinitionSupport() {}

	public final static > getDefinition( Class cls, int index ) {
		return cls.getEnumConstants()[index];
	}

	public final static > String getTitle(T columnDef) {
		return columnDef instanceof ITableColumDefinition?
                     ((ITableColumDefinition)columnDef).getTitle(): enumToName(columnDef);
	}

	private static > String enumToName(T columnDef) {
		return columnDef == null? null:
		       StringUtils.capitalize( columnDef.name().replace('_', ' ').toLowerCase());
	}

}

Advertisements

5 comments

  1. I use something similar:

    static private int COLUMNIDX = 0;
    final static public int COLUMNIDX_AMOUNT = COLUMNIDX++;
    final static public int COLUMNIDX_ARTICLE = COLUMNIDX++;

    final static public int COLUMNIDX_LICENSE_STARTENDDATE = COLUMNIDX++;
    final static public int COLUMNCNT = COLUMNIDX++;

    public int getColumnCount()
    {
    return COLUMNCNT;
    }

    public String getColumnName(int c)
    {
    if (c == COLUMNIDX_AMOUNT) return …;
    if (c == COLUMNIDX_ARTICLE) return …;

    And the same for the rest. You can now reorder the columns by simple move the column index assign.

    Another handy side effect is that if you even need to manually call “getValueAt”, you can refer to these COLUMNIDX variables.

    And last you can also include negative COLUMNIDX values and use that to get stuff from the TableModel that is not shown as a column, for example the backing entity:

    final static public int COLUMNIDX_ENTITY = -1;

    You can now ask the table model to return the entity in the getValueAt that provides the cell contents with its fields.

  2. @Tom
    I see a value in ability to have negative indexes. But haven’t had a need for this kind of thing yet. Your approach is very close to what I’m doing, except I think mine requires less code 🙂


Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s