javax.swing.table

Class DefaultTableColumnModel

public class DefaultTableColumnModel extends Object implements TableColumnModel, PropertyChangeListener, ListSelectionListener, Serializable

A model that stores information about the columns used in a {@link JTable}.

See Also:

Field Summary
protected ChangeEventchangeEvent
A change event used when notifying listeners of a change to the columnMargin field.
protected intcolumnMargin
The space between the columns (the default value is 1).
protected booleancolumnSelectionAllowed
A flag that indicates whether or not columns can be selected.
protected EventListenerListlistenerList
Storage for the listeners registered with the model.
protected ListSelectionModelselectionModel
A selection model that keeps track of column selections.
protected Vector<TableColumn>tableColumns
Storage for the table columns.
protected inttotalColumnWidth
The total width of all the columns in this model.
Constructor Summary
DefaultTableColumnModel()
Creates a new table column model with zero columns.
Method Summary
voidaddColumn(TableColumn column)
Adds a column to the model then calls {@link #fireColumnAdded(TableColumnModelEvent)} to notify the registered listeners.
voidaddColumnModelListener(TableColumnModelListener listener)
Registers a listener with the model, so that it will receive {@link TableColumnModelEvent} notifications.
protected ListSelectionModelcreateSelectionModel()
Creates a default selection model to track the currently selected column(s).
protected voidfireColumnAdded(TableColumnModelEvent e)
Sends the specified {@link TableColumnModelEvent} to all registered listeners, to indicate that a column has been added to the model.
protected voidfireColumnMarginChanged()
Sends a {@link ChangeEvent} to the model's registered listeners to indicate that the column margin was changed.
protected voidfireColumnMoved(TableColumnModelEvent e)
Sends the specified {@link TableColumnModelEvent} to all registered listeners, to indicate that a column in the model has been moved.
protected voidfireColumnRemoved(TableColumnModelEvent e)
Sends the specified {@link TableColumnModelEvent} to all registered listeners, to indicate that a column has been removed from the model.
protected voidfireColumnSelectionChanged(ListSelectionEvent e)
Sends the specified {@link ListSelectionEvent} to all registered listeners, to indicate that the column selections have changed.
TableColumngetColumn(int columnIndex)
Returns the column at the specified index.
intgetColumnCount()
Returns the number of columns in the model.
intgetColumnIndex(Object identifier)
Returns the index of the {@link TableColumn} with the given identifier.
intgetColumnIndexAtX(int x)
Returns the index of the column that contains the specified x-coordinate.
intgetColumnMargin()
Returns the column margin.
TableColumnModelListener[]getColumnModelListeners()
Returns an array containing the listeners that are registered with the model.
Enumeration<TableColumn>getColumns()
Returns an enumeration of the columns in the model.
booleangetColumnSelectionAllowed()
Returns true if column selection is allowed, and false if column selection is not allowed.
<T extends EventListener> T[]getListeners(Class<T> listenerType)
Returns an array containing the listeners (of the specified type) that are registered with this model.
intgetSelectedColumnCount()
Returns the number of selected columns in the model.
int[]getSelectedColumns()
Returns an array containing the indices of the selected columns.
ListSelectionModelgetSelectionModel()
Returns the selection model used to track table column selections.
intgetTotalColumnWidth()
Returns total width of all the columns in the model, ignoring the {@link #columnMargin}.
voidmoveColumn(int i, int j)
Moves the column at index i to the position specified by index j, then calls {@link #fireColumnMoved(TableColumnModelEvent)} to notify registered listeners.
voidpropertyChange(PropertyChangeEvent event)
Receives notification of property changes for the columns in the model.
protected voidrecalcWidthCache()
Recalculates the total width of the columns, if the cached value is -1.
voidremoveColumn(TableColumn column)
Removes a column from the model then calls {@link #fireColumnRemoved(TableColumnModelEvent)} to notify the registered listeners.
voidremoveColumnModelListener(TableColumnModelListener listener)
Deregisters a listener so that it no longer receives notification of changes to this model.
voidsetColumnMargin(int margin)
Sets the column margin then calls {@link #fireColumnMarginChanged()} to notify the registered listeners.
voidsetColumnSelectionAllowed(boolean flag)
Sets the flag that indicates whether or not column selection is allowed.
voidsetSelectionModel(ListSelectionModel model)
Sets the selection model that will be used to keep track of the selected columns.
voidvalueChanged(ListSelectionEvent e)
Receives notification of the change to the list selection model, and responds by calling {@link #fireColumnSelectionChanged(ListSelectionEvent)}.

Field Detail

changeEvent

protected transient ChangeEvent changeEvent
A change event used when notifying listeners of a change to the columnMargin field. This single event is reused for all notifications (it is lazily instantiated within the {@link #fireColumnMarginChanged()} method).

columnMargin

protected int columnMargin
The space between the columns (the default value is 1).

columnSelectionAllowed

protected boolean columnSelectionAllowed
A flag that indicates whether or not columns can be selected.

listenerList

protected EventListenerList listenerList
Storage for the listeners registered with the model.

selectionModel

protected ListSelectionModel selectionModel
A selection model that keeps track of column selections.

tableColumns

protected Vector<TableColumn> tableColumns
Storage for the table columns.

totalColumnWidth

protected int totalColumnWidth
The total width of all the columns in this model.

Constructor Detail

DefaultTableColumnModel

public DefaultTableColumnModel()
Creates a new table column model with zero columns. A default column selection model is created by calling {@link #createSelectionModel()}. The default value for columnMargin is 1 and the default value for columnSelectionAllowed is false.

Method Detail

addColumn

public void addColumn(TableColumn column)
Adds a column to the model then calls {@link #fireColumnAdded(TableColumnModelEvent)} to notify the registered listeners. The model registers itself with the column as a {@link PropertyChangeListener} so that changes to the column width will invalidate the cached {@link #totalColumnWidth} value.

Parameters: column the column (null not permitted).

Throws: IllegalArgumentException if column is null.

See Also: removeColumn

addColumnModelListener

public void addColumnModelListener(TableColumnModelListener listener)
Registers a listener with the model, so that it will receive {@link TableColumnModelEvent} notifications.

Parameters: listener the listener (null ignored).

createSelectionModel

protected ListSelectionModel createSelectionModel()
Creates a default selection model to track the currently selected column(s). This method is called by the constructor and returns a new instance of {@link DefaultListSelectionModel}.

Returns: A new default column selection model.

fireColumnAdded

protected void fireColumnAdded(TableColumnModelEvent e)
Sends the specified {@link TableColumnModelEvent} to all registered listeners, to indicate that a column has been added to the model. The event's toIndex attribute should contain the index of the added column.

Parameters: e the event.

See Also: addColumn

fireColumnMarginChanged

protected void fireColumnMarginChanged()
Sends a {@link ChangeEvent} to the model's registered listeners to indicate that the column margin was changed.

See Also: DefaultTableColumnModel

fireColumnMoved

protected void fireColumnMoved(TableColumnModelEvent e)
Sends the specified {@link TableColumnModelEvent} to all registered listeners, to indicate that a column in the model has been moved. The event's fromIndex attribute should contain the old column index, and the toIndex attribute should contain the new column index.

Parameters: e the event.

See Also: DefaultTableColumnModel

fireColumnRemoved

protected void fireColumnRemoved(TableColumnModelEvent e)
Sends the specified {@link TableColumnModelEvent} to all registered listeners, to indicate that a column has been removed from the model. The event's fromIndex attribute should contain the index of the removed column.

Parameters: e the event.

See Also: removeColumn

fireColumnSelectionChanged

protected void fireColumnSelectionChanged(ListSelectionEvent e)
Sends the specified {@link ListSelectionEvent} to all registered listeners, to indicate that the column selections have changed.

Parameters: e the event.

See Also: valueChanged

getColumn

public TableColumn getColumn(int columnIndex)
Returns the column at the specified index.

Parameters: columnIndex the column index (in the range from 0 to N-1, where N is the number of columns in the model).

Returns: The column at the specified index.

Throws: ArrayIndexOutOfBoundsException if i is not within the specified range.

getColumnCount

public int getColumnCount()
Returns the number of columns in the model.

Returns: The column count.

getColumnIndex

public int getColumnIndex(Object identifier)
Returns the index of the {@link TableColumn} with the given identifier.

Parameters: identifier the identifier (null not permitted).

Returns: The index of the {@link TableColumn} with the given identifier.

Throws: IllegalArgumentException if identifier is null or there is no column with that identifier.

getColumnIndexAtX

public int getColumnIndexAtX(int x)
Returns the index of the column that contains the specified x-coordinate. This method assumes that: If no column contains the specified position, this method returns -1.

Parameters: x the x-position.

Returns: The column index, or -1.

getColumnMargin

public int getColumnMargin()
Returns the column margin.

Returns: The column margin.

See Also: DefaultTableColumnModel

getColumnModelListeners

public TableColumnModelListener[] getColumnModelListeners()
Returns an array containing the listeners that are registered with the model. If there are no listeners, an empty array is returned.

Returns: An array containing the listeners that are registered with the model.

Since: 1.4

See Also: addColumnModelListener

getColumns

public Enumeration<TableColumn> getColumns()
Returns an enumeration of the columns in the model.

Returns: An enumeration of the columns in the model.

getColumnSelectionAllowed

public boolean getColumnSelectionAllowed()
Returns true if column selection is allowed, and false if column selection is not allowed.

Returns: A boolean.

See Also: DefaultTableColumnModel

getListeners

public <T extends EventListener> T[] getListeners(Class<T> listenerType)
Returns an array containing the listeners (of the specified type) that are registered with this model.

Parameters: listenerType the listener type (must indicate a subclass of {@link EventListener}, null not permitted).

Returns: An array containing the listeners (of the specified type) that are registered with this model.

getSelectedColumnCount

public int getSelectedColumnCount()
Returns the number of selected columns in the model.

Returns: The selected column count.

See Also: getSelectionModel

getSelectedColumns

public int[] getSelectedColumns()
Returns an array containing the indices of the selected columns.

Returns: An array containing the indices of the selected columns.

getSelectionModel

public ListSelectionModel getSelectionModel()
Returns the selection model used to track table column selections.

Returns: The selection model.

See Also: setSelectionModel

getTotalColumnWidth

public int getTotalColumnWidth()
Returns total width of all the columns in the model, ignoring the {@link #columnMargin}.

Returns: The total width of all the columns.

moveColumn

public void moveColumn(int i, int j)
Moves the column at index i to the position specified by index j, then calls {@link #fireColumnMoved(TableColumnModelEvent)} to notify registered listeners.

Parameters: i index of the column that will be moved. j index of the column's new location.

Throws: IllegalArgumentException if i or j are outside the range 0 to N-1, where N is the column count.

propertyChange

public void propertyChange(PropertyChangeEvent event)
Receives notification of property changes for the columns in the model. If the width property for any column changes, we invalidate the {@link #totalColumnWidth} value here.

Parameters: event the event.

recalcWidthCache

protected void recalcWidthCache()
Recalculates the total width of the columns, if the cached value is -1. Otherwise this method does nothing.

See Also: getTotalColumnWidth

removeColumn

public void removeColumn(TableColumn column)
Removes a column from the model then calls {@link #fireColumnRemoved(TableColumnModelEvent)} to notify the registered listeners. If the specified column does not belong to the model, or is null, this method does nothing.

Parameters: column the column to be removed (null permitted).

See Also: addColumn

removeColumnModelListener

public void removeColumnModelListener(TableColumnModelListener listener)
Deregisters a listener so that it no longer receives notification of changes to this model.

Parameters: listener the listener to remove

setColumnMargin

public void setColumnMargin(int margin)
Sets the column margin then calls {@link #fireColumnMarginChanged()} to notify the registered listeners.

Parameters: margin the column margin.

See Also: getColumnMargin

setColumnSelectionAllowed

public void setColumnSelectionAllowed(boolean flag)
Sets the flag that indicates whether or not column selection is allowed.

Parameters: flag the new flag value.

See Also: getColumnSelectionAllowed

setSelectionModel

public void setSelectionModel(ListSelectionModel model)
Sets the selection model that will be used to keep track of the selected columns.

Parameters: model the selection model (null not permitted).

Throws: IllegalArgumentException if model is null.

See Also: getSelectionModel

valueChanged

public void valueChanged(ListSelectionEvent e)
Receives notification of the change to the list selection model, and responds by calling {@link #fireColumnSelectionChanged(ListSelectionEvent)}.

Parameters: e the list selection event.

See Also: getSelectionModel