JavaTM 2 Platform
Std. Ed. v1.3.1

javax.swing.table
Class DefaultTableColumnModel

java.lang.Object
  |
  +--javax.swing.table.DefaultTableColumnModel
All Implemented Interfaces:
EventListener, ListSelectionListener, PropertyChangeListener, Serializable, TableColumnModel

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

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. A future release of Swing will provide support for long term persistence.

See Also:
JTable, Serialized Form

Field Summary
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 tableColumns
          Array of TableColumn objects in this model
protected  int totalColumnWidth
          A local cache of the combined width of all columns
 
Constructor Summary
DefaultTableColumnModel()
          Creates a default table column model.
 
Method Summary
 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.
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.
protected  void fireColumnMarginChanged()
          Notifies all listeners that have registered interest for notification on this event type.
protected  void fireColumnMoved(TableColumnModelEvent e)
          Notifies all listeners that have registered interest for notification on this event type.
protected  void fireColumnRemoved(TableColumnModelEvent e)
          Notifies all listeners that have registered interest for notification on this event type.
protected  void fireColumnSelectionChanged(ListSelectionEvent e)
          Notifies all listeners that have registered interest for notification on this event type.
 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.
 Enumeration getColumns()
          Returns an Enumeration of all the columns in the model.
 boolean getColumnSelectionAllowed()
          Returns true if column selection is allowed, otherwise false.
 EventListener[] getListeners(Class listenerType)
          Returns an array of all the listeners of the given type that were added to 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.
protected  void recalcWidthCache()
          Recalculates the total combined width of all columns.
 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.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

tableColumns

protected Vector tableColumns
Array of TableColumn objects in this model

selectionModel

protected ListSelectionModel selectionModel
Model for keeping track of column selections

columnMargin

protected int columnMargin
Width margin between each column

listenerList

protected EventListenerList listenerList
List of TableColumnModelListener

changeEvent

protected transient ChangeEvent changeEvent
Change event (only one needed)

columnSelectionAllowed

protected boolean columnSelectionAllowed
Column selection allowed in this column model

totalColumnWidth

protected int totalColumnWidth
A local cache of the combined width of all columns
Constructor Detail

DefaultTableColumnModel

public DefaultTableColumnModel()
Creates a default table column model.
Method Detail

addColumn

public void addColumn(TableColumn aColumn)
Appends aColumn to the end of the tableColumns array. This method also posts the columnAdded event to its listeners.
Specified by:
addColumn in interface TableColumnModel
Parameters:
column - the TableColumn to be added
Throws:
IllegalArgumentException - if aColumn is null
See Also:
removeColumn(javax.swing.table.TableColumn)

removeColumn

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.
Specified by:
removeColumn in interface TableColumnModel
Parameters:
column - the TableColumn to be removed
See Also:
addColumn(javax.swing.table.TableColumn)

moveColumn

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.
Specified by:
moveColumn in interface TableColumnModel
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

setColumnMargin

public void setColumnMargin(int newMargin)
Sets the column margin to newMargin. This method also posts a columnMarginChanged event to its listeners.
Specified by:
setColumnMargin in interface TableColumnModel
Parameters:
newMargin - the new margin width, in pixels
See Also:
getColumnMargin(), getTotalColumnWidth()

getColumnCount

public int getColumnCount()
Returns the number of columns in the tableColumns array.
Specified by:
getColumnCount in interface TableColumnModel
Returns:
the number of columns in the tableColumns array
See Also:
getColumns()

getColumns

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

getColumnIndex

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.
Specified by:
getColumnIndex in interface TableColumnModel
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:
getColumn(int)

getColumn

public TableColumn getColumn(int columnIndex)
Returns the TableColumn object for the column at columnIndex.
Specified by:
getColumn in interface TableColumnModel
Parameters:
columnIndex - the index of the column desired
Returns:
the TableColumn object for the column at columnIndex

getColumnMargin

public int getColumnMargin()
Returns the width margin for TableColumn. The default columnMargin is 1.
Specified by:
getColumnMargin in interface TableColumnModel
Returns:
the maximum width for the TableColumn
See Also:
setColumnMargin(int)

getColumnIndexAtX

public int getColumnIndexAtX(int x)
Returns the index of the column that lies at position x, or -1 if no column covers this point.
Specified by:
getColumnIndexAtX in interface TableColumnModel
Parameters:
x - the horizontal location of interest
Returns:
the index of the column or -1 if no column is found

getTotalColumnWidth

public int getTotalColumnWidth()
Returns the total combined width of all columns.
Specified by:
getTotalColumnWidth in interface TableColumnModel
Returns:
the totalColumnWidth property

setSelectionModel

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.
Specified by:
setSelectionModel in interface TableColumnModel
Parameters:
newModel - the new selection model
Throws:
IllegalArgumentException - if newModel is null
See Also:
getSelectionModel()

getSelectionModel

public ListSelectionModel getSelectionModel()
Returns the ListSelectionModel that is used to maintain column selection state.
Specified by:
getSelectionModel in interface TableColumnModel
Returns:
the object that provides column selection state. Or null if row selection is not allowed.
See Also:
setSelectionModel(javax.swing.ListSelectionModel)

setColumnSelectionAllowed

public void setColumnSelectionAllowed(boolean flag)
Sets whether column selection is allowed. The default is false.
Specified by:
setColumnSelectionAllowed in interface TableColumnModel
Parameters:
true - if column selection will be allowed, false otherwise

getColumnSelectionAllowed

public boolean getColumnSelectionAllowed()
Returns true if column selection is allowed, otherwise false. The default is false.
Specified by:
getColumnSelectionAllowed in interface TableColumnModel
Returns:
the columnSelectionAllowed property

getSelectedColumns

public int[] getSelectedColumns()
Returns an array of selected columns. If selectionModel is null, returns an empty array.
Specified by:
getSelectedColumns in interface TableColumnModel
Returns:
an array of selected columns or an empty array if nothing is selected or the selectionModel is null

getSelectedColumnCount

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

addColumnModelListener

public void addColumnModelListener(TableColumnModelListener x)
Adds a listener for table column model events.
Specified by:
addColumnModelListener in interface TableColumnModel
Parameters:
x - a TableColumnModelListener object

removeColumnModelListener

public void removeColumnModelListener(TableColumnModelListener x)
Removes a listener for table column model events.
Specified by:
removeColumnModelListener in interface TableColumnModel
Parameters:
x - a TableColumnModelListener object

fireColumnAdded

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
See Also:
EventListenerList

fireColumnRemoved

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
See Also:
EventListenerList

fireColumnMoved

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
See Also:
EventListenerList

fireColumnSelectionChanged

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
See Also:
EventListenerList

fireColumnMarginChanged

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.
Parameters:
e - the event received
See Also:
EventListenerList

getListeners

public EventListener[] getListeners(Class listenerType)
Returns an array of all the listeners of the given type that were added to this model.
Parameters:
listenerType - the listener class to match
Returns:
all of the objects receiving listenerType notifications from this model
Since:
1.3

propertyChange

public void propertyChange(PropertyChangeEvent evt)
Property Change Listener change method. Used to track changes to the column width or preferred column width.
Specified by:
propertyChange in interface PropertyChangeListener
Parameters:
evt - PropertyChangeEvent

valueChanged

public void valueChanged(ListSelectionEvent e)
A ListSelectionListener that forwards ListSelectionEvents when there is a column selection change.
Specified by:
valueChanged in interface ListSelectionListener
Parameters:
e - the change event

createSelectionModel

protected ListSelectionModel createSelectionModel()
Creates a new default list selection model.

recalcWidthCache

protected void recalcWidthCache()
Recalculates the total combined width of all columns. Updates the totalColumnWidth property.

JavaTM 2 Platform
Std. Ed. v1.3.1

Submit a bug or feature
For further API reference and developer documentation, see Java 2 SDK SE Developer Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.

Java, Java 2D, and JDBC are trademarks or registered trademarks of Sun Microsystems, Inc. in the US and other countries.
Copyright 1993-2001 Sun Microsystems, Inc. 901 San Antonio Road
Palo Alto, California, 94303, U.S.A. All Rights Reserved.