public class

DefaultTableColumnModel

extends Object
implements PropertyChangeListener Serializable ListSelectionListener TableColumnModel
java.lang.Object
   ↳ javax.swing.table.DefaultTableColumnModel

Class Overview

The standard column-handler for a JTable.

Warning: Serialized objects of this class will not be compatible with future Swing releases. The current serialization support is appropriate for short term storage or RMI between applications running the same version of Swing. As of 1.4, support for long term storage of all JavaBeansTM has been added to the java.beans package. Please see XMLEncoder.

See Also

Summary

Fields
protected ChangeEvent changeEvent Change event (only one needed)
protected int columnMargin Width margin between each column
protected boolean columnSelectionAllowed Column selection allowed in this column model
protected EventListenerList listenerList List of TableColumnModelListener
protected ListSelectionModel selectionModel Model for keeping track of column selections
protected Vector<TableColumn> tableColumns Array of TableColumn objects in this model
protected int totalColumnWidth A local cache of the combined width of all columns
Public Constructors
DefaultTableColumnModel()
Creates a default table column model.
Public Methods
void addColumn(TableColumn aColumn)
Appends aColumn to the end of the tableColumns array.
void addColumnModelListener(TableColumnModelListener x)
Adds a listener for table column model events.
TableColumn getColumn(int columnIndex)
Returns the TableColumn object for the column at columnIndex.
int getColumnCount()
Returns the number of columns in the tableColumns array.
int getColumnIndex(Object identifier)
Returns the index of the first column in the tableColumns array whose identifier is equal to identifier, when compared using equals.
int getColumnIndexAtX(int x)
Returns the index of the column that lies at position x, or -1 if no column covers this point.
int getColumnMargin()
Returns the width margin for TableColumn.
TableColumnModelListener[] getColumnModelListeners()
Returns an array of all the column model listeners registered on this model.
boolean getColumnSelectionAllowed()
Returns true if column selection is allowed, otherwise false.
Enumeration<TableColumn> getColumns()
Returns an Enumeration of all the columns in the model.
<T extends EventListener> T[] getListeners(Class<T> listenerType)
Returns an array of all the objects currently registered as FooListeners upon this model.
int getSelectedColumnCount()
Returns the number of columns selected.
int[] getSelectedColumns()
Returns an array of selected columns.
ListSelectionModel getSelectionModel()
Returns the ListSelectionModel that is used to maintain column selection state.
int getTotalColumnWidth()
Returns the total combined width of all columns.
void moveColumn(int columnIndex, int newIndex)
Moves the column and heading at columnIndex to newIndex.
void propertyChange(PropertyChangeEvent evt)
Property Change Listener change method.
void removeColumn(TableColumn column)
Deletes the column from the tableColumns array.
void removeColumnModelListener(TableColumnModelListener x)
Removes a listener for table column model events.
void setColumnMargin(int newMargin)
Sets the column margin to newMargin.
void setColumnSelectionAllowed(boolean flag)
Sets whether column selection is allowed.
void setSelectionModel(ListSelectionModel newModel)
Sets the selection model for this TableColumnModel to newModel and registers for listener notifications from the new selection model.
void valueChanged(ListSelectionEvent e)
A ListSelectionListener that forwards ListSelectionEvents when there is a column selection change.
Protected Methods
ListSelectionModel createSelectionModel()
Creates a new default list selection model.
void fireColumnAdded(TableColumnModelEvent e)
Notifies all listeners that have registered interest for notification on this event type.
void fireColumnMarginChanged()
Notifies all listeners that have registered interest for notification on this event type.
void fireColumnMoved(TableColumnModelEvent e)
Notifies all listeners that have registered interest for notification on this event type.
void fireColumnRemoved(TableColumnModelEvent e)
Notifies all listeners that have registered interest for notification on this event type.
void fireColumnSelectionChanged(ListSelectionEvent e)
Notifies all listeners that have registered interest for notification on this event type.
void recalcWidthCache()
Recalculates the total combined width of all columns.
[Expand]
Inherited Methods
From class java.lang.Object
From interface java.beans.PropertyChangeListener
From interface javax.swing.event.ListSelectionListener
From interface javax.swing.table.TableColumnModel

Fields

protected ChangeEvent changeEvent

Change event (only one needed)

protected int columnMargin

Width margin between each column

protected boolean columnSelectionAllowed

Column selection allowed in this column model

protected EventListenerList listenerList

List of TableColumnModelListener

protected ListSelectionModel selectionModel

Model for keeping track of column selections

protected Vector<TableColumn> tableColumns

Array of TableColumn objects in this model

protected int totalColumnWidth

A local cache of the combined width of all columns

Public Constructors

public DefaultTableColumnModel ()

Creates a default table column model.

Public Methods

public void addColumn (TableColumn aColumn)

Appends aColumn to the end of the tableColumns array. This method also posts the columnAdded event to its listeners.

Parameters
aColumn the TableColumn to be added
Throws
IllegalArgumentException if aColumn is null

public void addColumnModelListener (TableColumnModelListener x)

Adds a listener for table column model events.

Parameters
x a TableColumnModelListener object

public TableColumn getColumn (int columnIndex)

Returns the TableColumn object for the column at columnIndex.

Parameters
columnIndex the index of the column desired
Returns
  • the TableColumn object for the column at columnIndex

public int getColumnCount ()

Returns the number of columns in the tableColumns array.

Returns
  • the number of columns in the tableColumns array
See Also

public int getColumnIndex (Object identifier)

Returns the index of the first column in the tableColumns array whose identifier is equal to identifier, when compared using equals.

Parameters
identifier the identifier object
Returns
  • the index of the first column in the tableColumns array whose identifier is equal to identifier
Throws
IllegalArgumentException if identifier is null, or if no TableColumn has this identifier
See Also

public int getColumnIndexAtX (int x)

Returns the index of the column that lies at position x, or -1 if no column covers this point. In keeping with Swing's separable model architecture, a TableColumnModel does not know how the table columns actually appear on screen. The visual presentation of the columns is the responsibility of the view/controller object using this model (typically JTable). The view/controller need not display the columns sequentially from left to right. For example, columns could be displayed from right to left to accomodate a locale preference or some columns might be hidden at the request of the user. Because the model does not know how the columns are laid out on screen, the given xPosition should not be considered to be a coordinate in 2D graphics space. Instead, it should be considered to be a width from the start of the first column in the model. If the column index for a given X coordinate in 2D space is required, JTable.columnAtPoint can be used instead.

Parameters
x the horizontal location of interest
Returns
  • the index of the column or -1 if no column is found

public int getColumnMargin ()

Returns the width margin for TableColumn. The default columnMargin is 1.

Returns
  • the maximum width for the TableColumn

public TableColumnModelListener[] getColumnModelListeners ()

Returns an array of all the column model listeners registered on this model.

Returns
  • all of this default table column model's ColumnModelListeners or an empty array if no column model listeners are currently registered

public boolean getColumnSelectionAllowed ()

Returns true if column selection is allowed, otherwise false. The default is false.

Returns
  • the columnSelectionAllowed property

public Enumeration<TableColumn> getColumns ()

Returns an Enumeration of all the columns in the model.

Returns
  • an Enumeration of the columns in the model

public T[] getListeners (Class<T> listenerType)

Returns an array of all the objects currently registered as FooListeners upon this model. FooListeners are registered using the addFooListener method.

You can specify the listenerType argument with a class literal, such as FooListener.class. For example, you can query a DefaultTableColumnModel m for its column model listeners with the following code:

ColumnModelListener[] cmls = (ColumnModelListener[])(m.getListeners(ColumnModelListener.class));
If no such listeners exist, this method returns an empty array.

Parameters
listenerType the type of listeners requested; this parameter should specify an interface that descends from java.util.EventListener
Returns
  • an array of all objects registered as FooListeners on this model, or an empty array if no such listeners have been added
Throws
ClassCastException if listenerType doesn't specify a class or interface that implements java.util.EventListener

public int getSelectedColumnCount ()

Returns the number of columns selected.

Returns
  • the number of columns selected

public int[] getSelectedColumns ()

Returns an array of selected columns. If selectionModel is null, returns an empty array.

Returns
  • an array of selected columns or an empty array if nothing is selected or the selectionModel is null

public ListSelectionModel getSelectionModel ()

Returns the ListSelectionModel that is used to maintain column selection state.

Returns
  • the object that provides column selection state. Or null if row selection is not allowed.

public int getTotalColumnWidth ()

Returns the total combined width of all columns.

Returns
  • the totalColumnWidth property

public void moveColumn (int columnIndex, int newIndex)

Moves the column and heading at columnIndex to newIndex. The old column at columnIndex will now be found at newIndex. The column that used to be at newIndex is shifted left or right to make room. This will not move any columns if columnIndex equals newIndex. This method also posts a columnMoved event to its listeners.

Parameters
columnIndex the index of column to be moved
newIndex new index to move the column
Throws
IllegalArgumentException if column or newIndex are not in the valid range

public void propertyChange (PropertyChangeEvent evt)

Property Change Listener change method. Used to track changes to the column width or preferred column width.

Parameters
evt PropertyChangeEvent

public void removeColumn (TableColumn column)

Deletes the column from the tableColumns array. This method will do nothing if column is not in the table's columns list. tile is called to resize both the header and table views. This method also posts a columnRemoved event to its listeners.

Parameters
column the TableColumn to be removed

public void removeColumnModelListener (TableColumnModelListener x)

Removes a listener for table column model events.

Parameters
x a TableColumnModelListener object

public void setColumnMargin (int newMargin)

Sets the column margin to newMargin. This method also posts a columnMarginChanged event to its listeners.

Parameters
newMargin the new margin width, in pixels

public void setColumnSelectionAllowed (boolean flag)

Sets whether column selection is allowed. The default is false.

Parameters
flag true if column selection will be allowed, false otherwise

public void setSelectionModel (ListSelectionModel newModel)

Sets the selection model for this TableColumnModel to newModel and registers for listener notifications from the new selection model. If newModel is null, an exception is thrown.

Parameters
newModel the new selection model
Throws
IllegalArgumentException if newModel is null

public void valueChanged (ListSelectionEvent e)

A ListSelectionListener that forwards ListSelectionEvents when there is a column selection change.

Parameters
e the change event

Protected Methods

protected ListSelectionModel createSelectionModel ()

Creates a new default list selection model.

protected void fireColumnAdded (TableColumnModelEvent e)

Notifies all listeners that have registered interest for notification on this event type. The event instance is lazily created using the parameters passed into the fire method.

Parameters
e the event received

protected void fireColumnMarginChanged ()

Notifies all listeners that have registered interest for notification on this event type. The event instance is lazily created using the parameters passed into the fire method.

protected void fireColumnMoved (TableColumnModelEvent e)

Notifies all listeners that have registered interest for notification on this event type. The event instance is lazily created using the parameters passed into the fire method.

Parameters
e the event received

protected void fireColumnRemoved (TableColumnModelEvent e)

Notifies all listeners that have registered interest for notification on this event type. The event instance is lazily created using the parameters passed into the fire method.

Parameters
e the event received

protected void fireColumnSelectionChanged (ListSelectionEvent e)

Notifies all listeners that have registered interest for notification on this event type. The event instance is lazily created using the parameters passed into the fire method.

Parameters
e the event received

protected void recalcWidthCache ()

Recalculates the total combined width of all columns. Updates the totalColumnWidth property.