Class DefaultTableColumnModel
java.lang.Object
javax.swing.table.DefaultTableColumnModel
- All Implemented Interfaces:
- PropertyChangeListener,- Serializable,- EventListener,- ListSelectionListener,- 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.  As of 1.4, support for long term storage
 of all JavaBeans
 has been added to the java.beans package.
 Please see XMLEncoder.
- See Also:
- 
Field SummaryFieldsModifier and TypeFieldDescriptionprotected ChangeEventChange event (only one needed)protected intWidth margin between each columnprotected booleanColumn selection allowed in this column modelprotected EventListenerListList of TableColumnModelListenerprotected ListSelectionModelModel for keeping track of column selectionsprotected Vector<TableColumn> Array of TableColumn objects in this modelprotected intA local cache of the combined width of all columns
- 
Constructor SummaryConstructors
- 
Method SummaryModifier and TypeMethodDescriptionvoidaddColumn(TableColumn aColumn) AppendsaColumnto the end of thetableColumnsarray.voidAdds a listener for table column model events.protected ListSelectionModelCreates a new default list selection model.protected voidNotifies all listeners that have registered interest for notification on this event type.protected voidNotifies all listeners that have registered interest for notification on this event type.protected voidNotifies all listeners that have registered interest for notification on this event type.protected voidNotifies all listeners that have registered interest for notification on this event type.protected voidNotifies all listeners that have registered interest for notification on this event type.getColumn(int columnIndex) Returns theTableColumnobject for the column atcolumnIndex.intReturns the number of columns in thetableColumnsarray.intgetColumnIndex(Object identifier) Returns the index of the first column in thetableColumnsarray whose identifier is equal toidentifier, when compared usingequals.intgetColumnIndexAtX(int x) Returns the index of the column that lies at positionx, or -1 if no column covers this point.intReturns the width margin forTableColumn.Returns an array of all the column model listeners registered on this model.Returns anEnumerationof all the columns in the model.booleanReturns true if column selection is allowed, otherwise false.<T extends EventListener>
 T[]getListeners(Class<T> listenerType) Returns an array of all the objects currently registered asFooListeners upon this model.intReturns the number of columns selected.int[]Returns an array of selected columns.Returns theListSelectionModelthat is used to maintain column selection state.intReturns the total combined width of all columns.voidmoveColumn(int columnIndex, int newIndex) Moves the column and heading atcolumnIndextonewIndex.voidProperty Change Listener change method.protected voidRecalculates the total combined width of all columns.voidremoveColumn(TableColumn column) Deletes thecolumnfrom thetableColumnsarray.voidRemoves a listener for table column model events.voidsetColumnMargin(int newMargin) Sets the column margin tonewMargin.voidsetColumnSelectionAllowed(boolean flag) Sets whether column selection is allowed.voidsetSelectionModel(ListSelectionModel newModel) Sets the selection model for thisTableColumnModeltonewModeland registers for listener notifications from the new selection model.voidAListSelectionListenerthat forwardsListSelectionEventswhen there is a column selection change.
- 
Field Details- 
tableColumnsArray of TableColumn objects in this model
- 
selectionModelModel for keeping track of column selections
- 
columnMarginprotected int columnMarginWidth margin between each column
- 
listenerListList of TableColumnModelListener
- 
changeEventChange event (only one needed)
- 
columnSelectionAllowedprotected boolean columnSelectionAllowedColumn selection allowed in this column model
- 
totalColumnWidthprotected int totalColumnWidthA local cache of the combined width of all columns
 
- 
- 
Constructor Details- 
DefaultTableColumnModelpublic DefaultTableColumnModel()Creates a default table column model.
 
- 
- 
Method Details- 
addColumnAppendsaColumnto the end of thetableColumnsarray. This method also posts thecolumnAddedevent to its listeners.- Specified by:
- addColumnin interface- TableColumnModel
- Parameters:
- aColumn- the- TableColumnto be added
- Throws:
- IllegalArgumentException- if- aColumnis- null
- See Also:
 
- 
removeColumnDeletes thecolumnfrom thetableColumnsarray. This method will do nothing ifcolumnis not in the table's columns list.tileis called to resize both the header and table views. This method also posts acolumnRemovedevent to its listeners.- Specified by:
- removeColumnin interface- TableColumnModel
- Parameters:
- column- the- TableColumnto be removed
- See Also:
 
- 
moveColumnpublic void moveColumn(int columnIndex, int newIndex) Moves the column and heading atcolumnIndextonewIndex. The old column atcolumnIndexwill now be found atnewIndex. The column that used to be atnewIndexis shifted left or right to make room. This will not move any columns ifcolumnIndexequalsnewIndex. This method also posts acolumnMovedevent to its listeners.- Specified by:
- moveColumnin interface- TableColumnModel
- Parameters:
- columnIndex- the index of column to be moved
- newIndex- new index to move the column
- Throws:
- IllegalArgumentException- if- columnor- newIndexare not in the valid range
 
- 
setColumnMarginpublic void setColumnMargin(int newMargin) Sets the column margin tonewMargin. This method also posts acolumnMarginChangedevent to its listeners.- Specified by:
- setColumnMarginin interface- TableColumnModel
- Parameters:
- newMargin- the new margin width, in pixels
- See Also:
 
- 
getColumnCountpublic int getColumnCount()Returns the number of columns in thetableColumnsarray.- Specified by:
- getColumnCountin interface- TableColumnModel
- Returns:
- the number of columns in the tableColumnsarray
- See Also:
 
- 
getColumnsReturns anEnumerationof all the columns in the model.- Specified by:
- getColumnsin interface- TableColumnModel
- Returns:
- an Enumerationof the columns in the model
 
- 
getColumnIndexReturns the index of the first column in thetableColumnsarray whose identifier is equal toidentifier, when compared usingequals.- Specified by:
- getColumnIndexin interface- TableColumnModel
- Parameters:
- identifier- the identifier object
- Returns:
- the index of the first column in the
                  tableColumnsarray whose identifier is equal toidentifier
- Throws:
- IllegalArgumentException- if- identifieris- null, or if no- TableColumnhas this- identifier
- See Also:
 
- 
getColumnReturns theTableColumnobject for the column atcolumnIndex.- Specified by:
- getColumnin interface- TableColumnModel
- Parameters:
- columnIndex- the index of the column desired
- Returns:
- the TableColumnobject for the column atcolumnIndex
- Throws:
- ArrayIndexOutOfBoundsException- if- columnIndexis out of range: (- columnIndex < 0 || columnIndex >= getColumnCount())
 
- 
getColumnMarginpublic int getColumnMargin()Returns the width margin forTableColumn. The defaultcolumnMarginis 1.- Specified by:
- getColumnMarginin interface- TableColumnModel
- Returns:
- the maximum width for the TableColumn
- See Also:
 
- 
getColumnIndexAtXpublic int getColumnIndexAtX(int x) Returns the index of the column that lies at positionx, 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 accommodate 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 givenxPositionshould 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.columnAtPointcan be used instead.- Specified by:
- getColumnIndexAtXin interface- TableColumnModel
- Parameters:
- x- the horizontal location of interest
- Returns:
- the index of the column or -1 if no column is found
- See Also:
 
- 
getTotalColumnWidthpublic int getTotalColumnWidth()Returns the total combined width of all columns.- Specified by:
- getTotalColumnWidthin interface- TableColumnModel
- Returns:
- the totalColumnWidthproperty
 
- 
setSelectionModelSets the selection model for thisTableColumnModeltonewModeland registers for listener notifications from the new selection model. IfnewModelisnull, an exception is thrown.- Specified by:
- setSelectionModelin interface- TableColumnModel
- Parameters:
- newModel- the new selection model
- Throws:
- IllegalArgumentException- if- newModelis- null
- See Also:
 
- 
getSelectionModelReturns theListSelectionModelthat is used to maintain column selection state.- Specified by:
- getSelectionModelin interface- TableColumnModel
- Returns:
- the object that provides column selection state.  Or
          nullif row selection is not allowed.
- See Also:
 
- 
setColumnSelectionAllowedpublic void setColumnSelectionAllowed(boolean flag) Sets whether column selection is allowed. The default is false.- Specified by:
- setColumnSelectionAllowedin interface- TableColumnModel
- Parameters:
- flag- true if column selection will be allowed, false otherwise
- See Also:
 
- 
getColumnSelectionAllowedpublic boolean getColumnSelectionAllowed()Returns true if column selection is allowed, otherwise false. The default is false.- Specified by:
- getColumnSelectionAllowedin interface- TableColumnModel
- Returns:
- the columnSelectionAllowedproperty
- See Also:
 
- 
getSelectedColumnspublic int[] getSelectedColumns()Returns an array of selected columns. IfselectionModelisnull, returns an empty array.- Specified by:
- getSelectedColumnsin interface- TableColumnModel
- Returns:
- an array of selected columns or an empty array if nothing
                  is selected or the selectionModelisnull
 
- 
getSelectedColumnCountpublic int getSelectedColumnCount()Returns the number of columns selected.- Specified by:
- getSelectedColumnCountin interface- TableColumnModel
- Returns:
- the number of columns selected
 
- 
addColumnModelListenerAdds a listener for table column model events.- Specified by:
- addColumnModelListenerin interface- TableColumnModel
- Parameters:
- x- a- TableColumnModelListenerobject
 
- 
removeColumnModelListenerRemoves a listener for table column model events.- Specified by:
- removeColumnModelListenerin interface- TableColumnModel
- Parameters:
- x- a- TableColumnModelListenerobject
 
- 
getColumnModelListenersReturns 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
- Since:
- 1.4
- See Also:
 
- 
fireColumnAddedNotifies 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:
 
- 
fireColumnRemovedNotifies 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:
 
- 
fireColumnMovedNotifies 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:
 
- 
fireColumnSelectionChangedNotifies 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:
 
- 
fireColumnMarginChangedprotected 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.- See Also:
 
- 
getListenersReturns an array of all the objects currently registered asFooListeners upon this model.FooListeners are registered using theaddFooListenermethod.You can specify the listenerTypeargument with a class literal, such asFooListener.class. For example, you can query aDefaultTableColumnModelmfor 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.- Type Parameters:
- T- the listener type
- Parameters:
- listenerType- the type of listeners requested
- 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- listenerTypedoesn't specify a class or interface that implements- java.util.EventListener
- Since:
- 1.3
- See Also:
 
- 
propertyChangeProperty Change Listener change method. Used to track changes to the column width or preferred column width.- Specified by:
- propertyChangein interface- PropertyChangeListener
- Parameters:
- evt-- PropertyChangeEvent
 
- 
valueChangedAListSelectionListenerthat forwardsListSelectionEventswhen there is a column selection change.- Specified by:
- valueChangedin interface- ListSelectionListener
- Parameters:
- e- the change event
 
- 
createSelectionModelCreates a new default list selection model.- Returns:
- a newly created default list selection model.
 
- 
recalcWidthCacheprotected void recalcWidthCache()Recalculates the total combined width of all columns. Updates thetotalColumnWidthproperty.
 
-