|
|
Table models on-the-fly using "Glazed Lists"
See Glazed Lists for details on the library. Utility class:
import java.util.List;
import javax.swing.table.TableModel;
import ca.odell.glazedlists.EventList;
import ca.odell.glazedlists.GlazedLists;
import ca.odell.glazedlists.gui.TableFormat;
import ca.odell.glazedlists.swing.EventTableModel;
/**
* Wrapper to simplify table model creation with help from
* <a h ref="http://publicobject.com/glazedlists/">Glazed Lists</a> library.
* @author Victor Ott
*/
public class ModelUtil
{
/**
* Build a read-only table model using glazed lists library. Columns not having a corresponding name
* are reachable via "table.getModel().getValueAt(r, c)", starting with index "table.getModel().getColumnCount()",
* but will not be displayed. This way renderers and editors might access additional data, but also have
* to posess extra knowledge of e.g. total amount of properties available.
* @param <T> data row type
* @param model the data
* @param clazz row class
* @param columnProperties names of bean properties which deliver the column values
* @param columnLabels columns labels, drives the amount of shown columns from 'columnProperties'
* @return TableModel
*/
@SuppressWarnings("unchecked")
public static <T> TableModel buildTableModel(List<T> model, Class<T> clazz, String[] columnProperties,
String[] columnLabels)
{
return buildTableModel(model, clazz, columnProperties, columnLabels, null);
}
/**
* Build a potentially writable table model using glazed lists library. Columns not having a corresponding name
* are reachable via "table.getModel().getValueAt(row, col)", starting with index "table.getModel().getColumnCount()",
* but will not be displayed. This way renderers and editors might access additional data, but also have
* to posess extra knowledge of e.g. total amount of properties available.
* @param <T> data row type
* @param model the data
* @param clazz row class
* @param columnProperties names of bean properties which deliver the column values
* @param columnLabels columns labels, drives the amount of shown columns from 'columnProperties'
* @param editableColumns true: column is editable, false: column is read-only
* @return TableModel
*/
@SuppressWarnings("unchecked")
public static <T> TableModel buildTableModel(List<T> model, Class<T> clazz, String[] columnProperties,
String[] columnLabels, boolean[] editableColumns)
{
EventList<T> rolesOfContextList = GlazedLists.eventList(model);
TableFormat<T> tableFormat;
if (editableColumns == null)
tableFormat = GlazedLists.tableFormat(clazz, columnProperties, columnLabels);
else
tableFormat = GlazedLists.tableFormat(clazz, columnProperties, columnLabels, editableColumns);
TableModel tableModel = new EventTableModel<T>(rolesOfContextList, tableFormat);
return tableModel;
}
/**
* Hidden Constructor
*/
private ModelUtil()
{
}
}
Usage:
[...]
// ////////////////////////////////////////////////////////////////////////////
List<DummyEntry> entries = new ArrayList<DummyEntry>(20);
entries.add(new DummyEntry(0, "peter", "parker", 25));
entries.add(new DummyEntry(1, "bruce", "banner", 11));
entries.add(new DummyEntry(2, "klark", "kent", 33));
entries.add(new DummyEntry(3, "bruce", "wayne", 7));
// ////////////////////////////////////////////////////////////////////////////
String[] columnProperties = new String[] { "fn", "ln", "age", "id" };
String[] columnLabels = new String[] { "First name", "Last name", "Age" };
boolean[] editableColumns = { true, true, false, false };
TableModel tableModel = ModelUtil.buildTableModel(entries, DummyEntry.class, columnProperties, columnLabels,
editableColumns);
XTable table = new XTable(tableModel);
[...]
Result: For completeness, here also a stripped part of the data bean definition:
public class DummyEntry
{
private int _id;
private String _fn;
private String _ln;
private int _age;
public DummyEntry()
{
}
public DummyEntry(int id, String fn, String ln, int age)
{
_id = id;
_fn = fn;
_ln = ln;
_age = age;
}
public int getId()
{
return _id;
}
public void setId(int id)
{
_id = id;
}
public String getFn()
{
return _fn;
}
[...]
--Victor Ott 27-Nov-2007 01:14 CET |
|
All contents copyright of the author. ©2007. JAMWiki Version 0.5.3 |
|