javax.swing.table

Class DefaultTableColumnModel

Implemented Interfaces:
EventListener, ListSelectionListener, PropertyChangeListener, Serializable, TableColumnModel

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

A model that stores information about the columns used in a JTable.
See Also:
JTable.setColumnModel(TableColumnModel), Serialized Form

Field Summary

protected ChangeEvent
changeEvent
A change event used when notifying listeners of a change to the columnMargin field.
protected int
columnMargin
The space between the columns (the default value is 1).
protected boolean
columnSelectionAllowed
A flag that indicates whether or not columns can be selected.
protected EventListenerList
listenerList
Storage for the listeners registered with the model.
protected ListSelectionModel
selectionModel
A selection model that keeps track of column selections.
protected Vector
tableColumns
Storage for the table columns.
protected int
totalColumnWidth
The total width of all the columns in this model.

Constructor Summary

DefaultTableColumnModel()
Creates a new table column model with zero columns.

Method Summary

void
addColumn(TableColumn column)
Adds a column to the model then calls fireColumnAdded(TableColumnModelEvent) to notify the registered listeners.
void
addColumnModelListener(TableColumnModelListener listener)
Registers a listener with the model, so that it will receive TableColumnModelEvent notifications.
protected ListSelectionModel
createSelectionModel()
Creates a default selection model to track the currently selected column(s).
extends EventListener> T[] getListeners(Class listenerType)
Returns an array containing the listeners (of the specified type) that are registered with this model.
protected void
fireColumnAdded(TableColumnModelEvent e)
Sends the specified TableColumnModelEvent to all registered listeners, to indicate that a column has been added to the model.
protected void
fireColumnMarginChanged()
Sends a ChangeEvent to the model's registered listeners to indicate that the column margin was changed.
protected void
fireColumnMoved(TableColumnModelEvent e)
Sends the specified TableColumnModelEvent to all registered listeners, to indicate that a column in the model has been moved.
protected void
fireColumnRemoved(TableColumnModelEvent e)
Sends the specified TableColumnModelEvent to all registered listeners, to indicate that a column has been removed from the model.
protected void
fireColumnSelectionChanged(ListSelectionEvent e)
Sends the specified ListSelectionEvent to all registered listeners, to indicate that the column selections have changed.
TableColumn
getColumn(int columnIndex)
Returns the column at the specified index.
int
getColumnCount()
Returns the number of columns in the model.
int
getColumnIndex(Object identifier)
Returns the index of the TableColumn with the given identifier.
int
getColumnIndexAtX(int x)
Returns the index of the column that contains the specified x-coordinate.
int
getColumnMargin()
Returns the column margin.
TableColumnModelListener[]
getColumnModelListeners()
Returns an array containing the listeners that are registered with the model.
boolean
getColumnSelectionAllowed()
Returns true if column selection is allowed, and false if column selection is not allowed.
Enumeration
getColumns()
Returns an enumeration of the columns in the model.
int
getSelectedColumnCount()
Returns the number of selected columns in the model.
int[]
getSelectedColumns()
Returns an array containing the indices of the selected columns.
ListSelectionModel
getSelectionModel()
Returns the selection model used to track table column selections.
int
getTotalColumnWidth()
Returns total width of all the columns in the model, ignoring the columnMargin.
void
moveColumn(int i, int j)
Moves the column at index i to the position specified by index j, then calls fireColumnMoved(TableColumnModelEvent) to notify registered listeners.
void
propertyChange(PropertyChangeEvent event)
Receives notification of property changes for the columns in the model.
protected void
recalcWidthCache()
Recalculates the total width of the columns, if the cached value is -1.
void
removeColumn(TableColumn column)
Removes a column from the model then calls fireColumnRemoved(TableColumnModelEvent) to notify the registered listeners.
void
removeColumnModelListener(TableColumnModelListener listener)
Deregisters a listener so that it no longer receives notification of changes to this model.
void
setColumnMargin(int margin)
Sets the column margin then calls fireColumnMarginChanged() to notify the registered listeners.
void
setColumnSelectionAllowed(boolean flag)
Sets the flag that indicates whether or not column selection is allowed.
void
setSelectionModel(ListSelectionModel model)
Sets the selection model that will be used to keep track of the selected columns.
void
valueChanged(ListSelectionEvent e)
Receives notification of the change to the list selection model, and responds by calling fireColumnSelectionChanged(ListSelectionEvent).

Methods inherited from class java.lang.Object

clone, equals, extends Object> getClass, finalize, hashCode, notify, notifyAll, toString, wait, wait, wait

Field Details

changeEvent

protected 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 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 tableColumns
Storage for the table columns.

totalColumnWidth

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

Constructor Details

DefaultTableColumnModel

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

Method Details

addColumn

public void addColumn(TableColumn column)
Adds a column to the model then calls fireColumnAdded(TableColumnModelEvent) to notify the registered listeners. The model registers itself with the column as a PropertyChangeListener so that changes to the column width will invalidate the cached totalColumnWidth value.
Specified by:
addColumn in interface TableColumnModel
Parameters:
column - the column (null not permitted).
Throws:
IllegalArgumentException - if column is null.

addColumnModelListener

public void addColumnModelListener(TableColumnModelListener listener)
Registers a listener with the model, so that it will receive TableColumnModelEvent notifications.
Specified by:
addColumnModelListener in interface TableColumnModel
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 DefaultListSelectionModel.
Returns:
A new default column selection model.

extends EventListener> T[] getListeners

public extends EventListener> T[] getListeners(Class 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 EventListener, null not permitted).
Returns:
An array containing the listeners (of the specified type) that are registered with this model.

fireColumnAdded

protected void fireColumnAdded(TableColumnModelEvent e)
Sends the specified 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.

fireColumnMarginChanged

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

fireColumnMoved

protected void fireColumnMoved(TableColumnModelEvent e)
Sends the specified 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.

fireColumnRemoved

protected void fireColumnRemoved(TableColumnModelEvent e)
Sends the specified 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.

fireColumnSelectionChanged

protected void fireColumnSelectionChanged(ListSelectionEvent e)
Sends the specified ListSelectionEvent to all registered listeners, to indicate that the column selections have changed.
Parameters:
e - the event.

getColumn

public TableColumn getColumn(int columnIndex)
Returns the column at the specified index.
Specified by:
getColumn in interface TableColumnModel
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.
Specified by:
getColumnCount in interface TableColumnModel
Returns:
The column count.

getColumnIndex

public int getColumnIndex(Object identifier)
Returns the index of the TableColumn with the given identifier.
Specified by:
getColumnIndex in interface TableColumnModel
Parameters:
identifier - the identifier (null not permitted).
Returns:
The index of the 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:
  • column zero begins at position zero;
  • all columns appear in order;
  • individual column widths are taken into account, but the column margin is ignored.
If no column contains the specified position, this method returns -1.
Specified by:
getColumnIndexAtX in interface TableColumnModel
Parameters:
x - the x-position.
Returns:
The column index, or -1.

getColumnMargin

public int getColumnMargin()
Returns the column margin.
Specified by:
getColumnMargin in interface TableColumnModel
Returns:
The column margin.

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

getColumnSelectionAllowed

public boolean getColumnSelectionAllowed()
Returns true if column selection is allowed, and false if column selection is not allowed.
Specified by:
getColumnSelectionAllowed in interface TableColumnModel
Returns:
A boolean.

getColumns

public Enumeration getColumns()
Returns an enumeration of the columns in the model.
Specified by:
getColumns in interface TableColumnModel
Returns:
An enumeration of the columns in the model.

getSelectedColumnCount

public int getSelectedColumnCount()
Returns the number of selected columns in the model.
Specified by:
getSelectedColumnCount in interface TableColumnModel
Returns:
The selected column count.

getSelectedColumns

public int[] getSelectedColumns()
Returns an array containing the indices of the selected columns.
Specified by:
getSelectedColumns in interface TableColumnModel
Returns:
An array containing the indices of the selected columns.

getSelectionModel

public ListSelectionModel getSelectionModel()
Returns the selection model used to track table column selections.
Specified by:
getSelectionModel in interface TableColumnModel
Returns:
The selection model.

getTotalColumnWidth

public int getTotalColumnWidth()
Returns total width of all the columns in the model, ignoring the columnMargin.
Specified by:
getTotalColumnWidth in interface TableColumnModel
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 fireColumnMoved(TableColumnModelEvent) to notify registered listeners.
Specified by:
moveColumn in interface TableColumnModel
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 totalColumnWidth value here.
Specified by:
propertyChange in interface PropertyChangeListener
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.

removeColumn

public void removeColumn(TableColumn column)
Removes a column from the model then calls fireColumnRemoved(TableColumnModelEvent) to notify the registered listeners. If the specified column does not belong to the model, or is null, this method does nothing.
Specified by:
removeColumn in interface TableColumnModel
Parameters:
column - the column to be removed (null permitted).

removeColumnModelListener

public void removeColumnModelListener(TableColumnModelListener listener)
Deregisters a listener so that it no longer receives notification of changes to this model.
Specified by:
removeColumnModelListener in interface TableColumnModel
Parameters:
listener - the listener to remove

setColumnMargin

public void setColumnMargin(int margin)
Sets the column margin then calls fireColumnMarginChanged() to notify the registered listeners.
Specified by:
setColumnMargin in interface TableColumnModel
Parameters:
margin - the column margin.

setColumnSelectionAllowed

public void setColumnSelectionAllowed(boolean flag)
Sets the flag that indicates whether or not column selection is allowed.
Specified by:
setColumnSelectionAllowed in interface TableColumnModel
Parameters:
flag - the new flag value.

setSelectionModel

public void setSelectionModel(ListSelectionModel model)
Sets the selection model that will be used to keep track of the selected columns.
Specified by:
setSelectionModel in interface TableColumnModel
Parameters:
model - the selection model (null not permitted).
Throws:
IllegalArgumentException - if model is null.

valueChanged

public void valueChanged(ListSelectionEvent e)
Receives notification of the change to the list selection model, and responds by calling fireColumnSelectionChanged(ListSelectionEvent).
Specified by:
valueChanged in interface ListSelectionListener
Parameters:
e - the list selection event.

DefaultTableColumnModel.java -- Copyright (C) 2002, 2004, 2005, 2006, Free Software Foundation, Inc. This file is part of GNU Classpath. GNU Classpath is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. GNU Classpath is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with GNU Classpath; see the file COPYING. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. Linking this library statically or dynamically with other modules is making a combined work based on this library. Thus, the terms and conditions of the GNU General Public License cover the whole combination. As a special exception, the copyright holders of this library give you permission to link this library with independent modules to produce an executable, regardless of the license terms of these independent modules, and to copy and distribute the resulting executable under terms of your choice, provided that you also meet, for each linked independent module, the terms and conditions of the license of that module. An independent module is a module which is not derived from or based on this library. If you modify this library, you may extend this exception to your version of the library, but you are not obligated to do so. If you do not wish to do so, delete this exception statement from your version.