Java Reference
In-Depth Information
EventQueue.invokeLater(runner);
}
}
TableColumn Class
TableColumn
is another important class that lives behind the scenes. Swing tables consist of
a group of columns, which are made up of cells. Each of those columns is described by a
TableColumn
instance. Each instance of the
TableColumn
class stores the appropriate editor,
renderer, name, and sizing information.
TableColumn
objects are then grouped together into a
TableColumnModel
to make up the current set of columns to be displayed by a
JTable
. One useful
trick to remember is if you don't want a column to be displayed, remove its
TableColumn
from
the
TableColumnModel
but leave it in the
TableModel
.
Creating a TableColumn
If you choose to create your
TableColumn
objects yourself, you can use any one of four constructors.
They cascade by adding more constructor arguments.
public TableColumn()
TableColumn column = new TableColumn()
public TableColumn(int modelIndex)
TableColumn column = new TableColumn(2)
public TableColumn(int modelIndex, int width)
TableColumn column = new TableColumn(2, 25)
public TableColumn(int modelIndex, int width, TableCellRenderer
renderer, TableCellEditor editor)
TableColumn column = new TableColumn(2, 25, aRenderer, aEditor)
■
Note
All column settings start at zero. Therefore,
new TableColumn(2)
uses column 3 from the
TableModel
.
With no arguments, such as in the first constructor in this list, you get an empty column
with a default width (75 pixels), a default editor, and a default renderer. The
modelIndex
argu-
ment allows you to specify which column from the
TableModel
you would like the
TableColumn
to display within the
JTable
. You can also specify a width, a renderer, or an editor if you don't
like the defaults. If you like one default but not the other, you can also specify
null
for the
renderer or editor.