TableModel editor

The TableModel Editor dialog
Feed a table with your own data

To sketch a layout precisely, the TableModel editor might be useful.
You can define the table's size, column titles, column types, column default values, column cell renderers, column header renderers and the table data, even cell renderers for individual table cells.

To open the editor, double-click a JTable or select 'Edit TableModel' from a table's .


The TableModel Editor dialog

Number of rows: 1 to 99.
Number of columns: 1 to 16. (Both the column-definition table and the data table have context menus allowing you to insert or remove columns.)
Title: Enter the column title.
Type: Select between Object, Boolean and Icon. (You can easily simulate the Number type with a right-aligned cell renderer and, maybe, a monospaced font)
Default value: Enter the default value for this column.
Editable: Select, if this column's data shall be editable or not.
Resizable: Select, if this column is resizable or not (non-resizable columns will be displayed at their preferred size and will not stretch). Enabled only with a table type of SelfResizingTable.
Column renderer: Define a TableCellRenderer for this column.
Header renderer: Define a for the header of this column.
Table data: Enter the data. If you right-click a table cell, you can define a cell renderer for this individual cell. (Note: Cell renderers for individual cells will only be remembered, if you select the SelfResizingTable type.)
Table type: You can choose between JTable and SelfResizingTable. SelfResizingTable is special in that it calculates the optimal column widths every time its tableChanged(TableModelEvent) method is called or its parent container is resized. (Please see the note below about performance issues)
Default Model: Set the default "A" | "B" | "C" 6 x 3 model and discard any custom cell renderers.

Note: SelfResizingTable isn't for free. It costs time to calculate column widths because every table cell is inspected for its preferred width. I used SelfResizingTable with table models containing up to 100,000 rows and found performance acceptable.
If you feed a SelfResizingTable with a table model of your own, you need to know that every TableModel method starting with fire finally will call tableChanged(TableModelEvent) and so trigger recalculation of column widths.
A SelfResizingTable will appear as type JTable in the exported class. This makes sense because it has no additional methods.
Further note, that autoResizeMode has no effect on SelfResizingTable.


Feed a table with your own data     Top of page

First, if you didn't define table cell renderers or header renderers for a table you can do whatever you want, set a new model for the table or completely redefine the table. Only if you want to preserve table cell renderers or header renderers read on because there are some restrictions.

You should know that RADi stores table data in a RadiTableModel which is derived from DefaultTableModel.

Suppose you have defined a table with 6 columns and 8 rows, then the following code will preserve your renderers (because it only manipulates row data):

// define some dummy data
Object[][] data = new Object[][] {
    {"01", "02", "03", "04", "05", "06"},
    {"07", "08", "09", "10", "11", "12"},
    {"13", "14", "15", "16", "17", "18"},
    {"19", "20", "21", "22", "23", "24"},
    {"25", "26", "27", "28", "29", "30"},
    {"31", "32", "33", "34", "35", "36"}
};

// fill the table
for(int i = 0; i < data.length; i++) {
    for(int j = 0; j < data[i].length; j++) {
        table.setValueAt(data[i][j], i, j);
    }
}

// insert a record (insert position must be <= rowCount)
((DefaultTableModel)table.getModel()).insertRow(6, data[2]);

// remove a record
((DefaultTableModel)table.getModel()).removeRow(2);

// move a record (new position must be < rowCount)
((DefaultTableModel)table.getModel()).moveRow(1, 1, 6);

// add a record to the end of the table
((DefaultTableModel)table.getModel()).addRow(
    new Object[] {"T", "h", "e", "E", "n", "d"}
);

// change the number of rows
((DefaultTableModel)table.getModel()).setRowCount(10);

These code examples will remove your renderers (because they manipulate the table's column model):

// add a new column
((DefaultTableModel)table.getModel()).addColumn("New");

// change the number of columns
((DefaultTableModel)table.getModel()).setColumnCount(8);

// set column names
((DefaultTableModel)table.getModel()).setColumnIdentifiers(
    new Object[] {"-A-", "-B-", "-C-", "-D-", "-E-", "-F-"});

// set a new data vector
((DefaultTableModel)table.getModel()).setDataVector(data,
    new Object[] {"-A-", "-B-", "-C-", "-D-", "-E-", "-F-"});

// set a new table model
table.setModel(new DataModel());