Using a Table Model

Using a Table Model instead of the default values will allow you to specify dynamic data, the advantages of this are numerous from special effects, dynamically updating data, cell editing etc.

There are two new steps:
  • Setting up a Custom Cell Renderer
  • Constructing E-Gantt with the TableModel constructor

Setting up the Cell Renderer

When using a Table Model though E-Gantt can not automatically calculate which columns should be specified as type AbstractDrawingState so the DrawingComponent will not be called and the default Object renderer will of course render the Object with it's toString value using the DefaultCellRender for Object.class.

This functionality is obviously not what we are looking for:

TableColumn column = columnModel.getColumn(0);
column.setCellRenderer(table.getDefaultRenderer(1, AbstractDrawingState.class));

You may wish to combine this with the setting up of the Column Header Value's for convience.

{
// we take the second tables column model as this is where we want to render
// the calendar header on top
TableColumnModel columnModel = table.getColumnModel(1);
 
// from this we then take the column note: there should only be one column in this table
TableColumn column = columnModel.getColumn(0);
column.setCellRenderer(table.getDefaultRenderer(1, AbstractDrawingState.class));
 
// We now set the header for this column to be the calendar drawing state
column.setHeaderValue(GanttEntryHelper.createCalendar());
}

Constructing E-Gantt with the TableModel constructor

To Contruct E-Gant with the Table Model constructor we have made the following changes to our previous examples
  • No longer specify data via the contructor the data is in the model
  • No longer specify column names via the contructor E-Gantt will designate the last column for the second table
String[] columnNames = {
"First Name", "Last Name", "Sport", "# of Years",
"Years Display [0..100]"};
 
DefaultTableModel model = new DefaultTableModel(columnNames, 0);
DrawingState entry = GanttEntryHelper.createActivityEntry(new Date(2005 - 1900, 0, new Date(2005 - 1900, e, 1));
Object values [] = new Object[] {"one", "two", "three", new Integer(i), entry};
model.addRow(values);
final GanttTable table = new GanttTable(model);

external image sflogo.php?group_id=49356