Java Reference
In-Depth Information
default column model from the data model, specifying that the display column order remains
in the data model order.
When the
autoCreateColumnsFromModel
property of
JTable
is set (
true
) prior to setting the
data model of the
JTable
, the
TableColumnModel
is automatically created. In addition, you can
manually tell the
JTable
to create the default
TableColumnModel
if the current settings need to
be reset. The
public void createDefaultColumnsFromModel()
method does the creation for
you, assigning the new creation to the
TableColumnModel
of the
JTable
.
With all that automatically done for you, why do you need to look at the
TableColumnModel
?
Usually, you'll need to work with this interface only when you don't like the defaults or when
you want to manually move things around. In addition to maintaining a set of
TableColumn
objects, the
TableColumnModel
manages a second
ListSelectionModel
, which allows users to
select columns and rows from the table.
Let's take a look at the interface definition before getting into the default implementation.
public interface TableColumnModel {
// Listeners
public void addColumnModelListener(TableColumnModelListener l);
public void removeColumnModelListener(TableColumnModelListener l);
// Properties
public int getColumnCount();
public int getColumnMargin();
public void setColumnMargin(int newMargin);
public Enumeration getColumns();
public boolean getColumnSelectionAllowed();
public void setColumnSelectionAllowed(boolean flag);
public int getSelectedColumnCount();
public int[ ] getSelectedColumns();
public ListSelectionModel getSelectionModel();
public void setSelectionModel(ListSelectionModel newModel);
public int getTotalColumnWidth();
// Other methods
public void addColumn(TableColumn aColumn);
public TableColumn getColumn(int columnIndex);
public int getColumnIndex(Object columnIdentifier);
public int getColumnIndexAtX(int xPosition);
public void moveColumn(int columnIndex, int newIndex);
public void removeColumn(TableColumn column);
}
DefaultTableColumnModel Class
The
DefaultTableColumnModel
class defines the implementation of the
TableColumnModel
inter-
face used by the system. It describes the general appearance of the
TableColumn
objects within
the
JTable
by tracking margins, width, selection, and quantity. Table 18-8 shows the nine
properties for accessing the
DefaultTableColumnModel
settings.