SOURCECODE |
How to... define a new TableLayout
Description:
The TableLayout is the definition of how the information provided by the entries of a data container, which in tables are being represented as records, shall be split and put into the cells.
As it is described in "How to create a table", this is what a TableEntryDescriptor deals with. It defines the number of columns the table shall have, the headers of the columns, which TableCellRenderer or TableCellEditor shall be used and which value goes into a column.
The most important thing in a TED is to return the right Classes, CellRenderer or CellEditors for each column, because the displayed value is being visualized by a JComponent and the different types are being processed by the Renderer or Editor in very different ways to cast them. Mistakes will oftenly cause ClassCastExceptions, followed by huge error messages concerning the table rendering.
All this is defined columnwise, because the framework processes the records and fills each row, representing a record, column by column, representing values from the record.
Our example is taken from the DefaultCatalogItemTED of the framework. It is called ..CatalogItem.. because it deals with the entries of a Catalog, which are CatalogItems. So it is normally used with a CatalogTableModel. For more information on this, please refer to "How to create a table".
ToDo's:
- Extend the AbstractTableEntryDescriptor which implements most of the methods of the interface TableEntryDescriptor quite sufficiently, so we only have to redefine those which do not fit to our data structure
- Call the superclass in the Constructor in order to initialize the TED properly
- Define the number of columns needed by redefining the method
public int getColumnCount()
. In this case it returns 2, because this TED only displays the name and value of the CatalogItem
- Define the header of the columns with the method
public String getColumnName(int index)
. Here an array of Strings is being used, but this can be done by anything that returns the right caption to the given index, beginning with '0' for the first column (if, selectCase or like in this example, an array)
- Define the class which each column shall deal with, ie. the class of the entrie's value that goes in that column, like in this case a String in the first and a value in the second one. Here, too, anything does, as long as it returns the right class to the right index
- Define the entry that goes into each column of a line in
public Object getValueAt(Object oData, int index)
The returned Object is of course the value to be written into column number 'index'. oData is the data container entry, which is the record associated to the processed line. As it is a CatalogItem in this case, it is casted to it and then it's metods are being used to resolve the input for the cells.
Add this TED to the AbstractTable instance of the Table you assemble and it will be able to display a Catalog by it's CatalogItems.
import data.*;
import util.swing.*;
1
public class DefaultCatalogItemTED extends AbstractTableEntryDescriptor {
2
public DefaultCatalogItemTED() {
super();
}
3
//here we need only two columns, one for the name of the CatalogItem and one for it's value
public int getColumnCount() {
return 2;
}
4
//the header of the columns, "Name" for the first, "Value" for the second one
public String getColumnName(int nIdx) {
String[] asNames = {"Name", "Value"};
return asNames[nIdx];
}
5
//The first column deals with Strings and the second one with Values.
//This is rather important, because wrong definitions will cause ClassCastExceptions
public Class getColumnClass(int nIdx) {
Class[] acClasses = {String.class, Value.class};
return acClasses[nIdx];
}
6
//Here the cell-entry is being resolved, column by column,
//taken from the Object oData, which is a CagtalogItem in this case
//The returned Object's class has to fit to the definition above.
public Object getValueAt(Object oData, int nIdx) {
if (nIdx == 0) {
return ((CatalogItem)oData).getName();
}
else {
return ((QuoteValue)((CatalogItem)oData).getValue()).getBid();
}
}
}