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:
JTable
-
Field Summary
Fields Modifier and Type Field Description protected ChangeEventchangeEventChange event (only one needed)protected intcolumnMarginWidth margin between each columnprotected booleancolumnSelectionAllowedColumn selection allowed in this column modelprotected EventListenerListlistenerListList of TableColumnModelListenerprotected ListSelectionModelselectionModelModel for keeping track of column selectionsprotected Vector<TableColumn>tableColumnsArray of TableColumn objects in this modelprotected inttotalColumnWidthA local cache of the combined width of all columns -
Constructor Summary
Constructors Constructor Description DefaultTableColumnModel()Creates a default table column model. -
Method Summary
Modifier and Type Method Description voidaddColumn(TableColumn aColumn)AppendsaColumnto the end of thetableColumnsarray.voidaddColumnModelListener(TableColumnModelListener x)Adds a listener for table column model events.protected ListSelectionModelcreateSelectionModel()Creates a new default list selection model.protected voidfireColumnAdded(TableColumnModelEvent e)Notifies all listeners that have registered interest for notification on this event type.protected voidfireColumnMarginChanged()Notifies all listeners that have registered interest for notification on this event type.protected voidfireColumnMoved(TableColumnModelEvent e)Notifies all listeners that have registered interest for notification on this event type.protected voidfireColumnRemoved(TableColumnModelEvent e)Notifies all listeners that have registered interest for notification on this event type.protected voidfireColumnSelectionChanged(ListSelectionEvent e)Notifies all listeners that have registered interest for notification on this event type.TableColumngetColumn(int columnIndex)Returns theTableColumnobject for the column atcolumnIndex.intgetColumnCount()Returns 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.intgetColumnMargin()Returns the width margin forTableColumn.TableColumnModelListener[]getColumnModelListeners()Returns an array of all the column model listeners registered on this model.Enumeration<TableColumn>getColumns()Returns anEnumerationof all the columns in the model.booleangetColumnSelectionAllowed()Returns 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.intgetSelectedColumnCount()Returns the number of columns selected.int[]getSelectedColumns()Returns an array of selected columns.ListSelectionModelgetSelectionModel()Returns theListSelectionModelthat is used to maintain column selection state.intgetTotalColumnWidth()Returns the total combined width of all columns.voidmoveColumn(int columnIndex, int newIndex)Moves the column and heading atcolumnIndextonewIndex.voidpropertyChange(PropertyChangeEvent evt)Property Change Listener change method.protected voidrecalcWidthCache()Recalculates the total combined width of all columns.voidremoveColumn(TableColumn column)Deletes thecolumnfrom thetableColumnsarray.voidremoveColumnModelListener(TableColumnModelListener x)Removes 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.voidvalueChanged(ListSelectionEvent e)AListSelectionListenerthat forwardsListSelectionEventswhen there is a column selection change.
-
Field Details
-
tableColumns
Array of TableColumn objects in this model -
selectionModel
Model for keeping track of column selections -
columnMargin
protected int columnMarginWidth margin between each column -
listenerList
List of TableColumnModelListener -
changeEvent
Change event (only one needed) -
columnSelectionAllowed
protected boolean columnSelectionAllowedColumn selection allowed in this column model -
totalColumnWidth
protected int totalColumnWidthA local cache of the combined width of all columns
-
-
Constructor Details
-
DefaultTableColumnModel
public DefaultTableColumnModel()Creates a default table column model.
-
-
Method Details
-
addColumn
AppendsaColumnto the end of thetableColumnsarray. This method also posts thecolumnAddedevent to its listeners.- Specified by:
addColumnin interfaceTableColumnModel- Parameters:
aColumn- theTableColumnto be added- Throws:
IllegalArgumentException- ifaColumnisnull- See Also:
removeColumn(javax.swing.table.TableColumn)
-
removeColumn
Deletes 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 interfaceTableColumnModel- Parameters:
column- theTableColumnto be removed- See Also:
addColumn(javax.swing.table.TableColumn)
-
moveColumn
public 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 interfaceTableColumnModel- Parameters:
columnIndex- the index of column to be movednewIndex- new index to move the column- Throws:
IllegalArgumentException- ifcolumnornewIndexare not in the valid range
-
setColumnMargin
public void setColumnMargin(int newMargin)Sets the column margin tonewMargin. This method also posts acolumnMarginChangedevent to its listeners.- Specified by:
setColumnMarginin interfaceTableColumnModel- Parameters:
newMargin- the new margin width, in pixels- See Also:
getColumnMargin(),getTotalColumnWidth()
-
getColumnCount
public int getColumnCount()Returns the number of columns in thetableColumnsarray.- Specified by:
getColumnCountin interfaceTableColumnModel- Returns:
- the number of columns in the
tableColumnsarray - See Also:
getColumns()
-
getColumns
Returns anEnumerationof all the columns in the model.- Specified by:
getColumnsin interfaceTableColumnModel- Returns:
- an
Enumerationof the columns in the model
-
getColumnIndex
Returns the index of the first column in thetableColumnsarray whose identifier is equal toidentifier, when compared usingequals.- Specified by:
getColumnIndexin interfaceTableColumnModel- Parameters:
identifier- the identifier object- Returns:
- the index of the first column in the
tableColumnsarray whose identifier is equal toidentifier - Throws:
IllegalArgumentException- ifidentifierisnull, or if noTableColumnhas thisidentifier- See Also:
getColumn(int)
-
getColumn
Returns theTableColumnobject for the column atcolumnIndex.- Specified by:
getColumnin interfaceTableColumnModel- Parameters:
columnIndex- the index of the column desired- Returns:
- the
TableColumnobject for the column atcolumnIndex
-
getColumnMargin
public int getColumnMargin()Returns the width margin forTableColumn. The defaultcolumnMarginis 1.- Specified by:
getColumnMarginin interfaceTableColumnModel- 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 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 interfaceTableColumnModel- Parameters:
x- the horizontal location of interest- Returns:
- the index of the column or -1 if no column is found
- See Also:
JTable.columnAtPoint(java.awt.Point)
-
getTotalColumnWidth
public int getTotalColumnWidth()Returns the total combined width of all columns.- Specified by:
getTotalColumnWidthin interfaceTableColumnModel- Returns:
- the
totalColumnWidthproperty
-
setSelectionModel
Sets the selection model for thisTableColumnModeltonewModeland registers for listener notifications from the new selection model. IfnewModelisnull, an exception is thrown.- Specified by:
setSelectionModelin interfaceTableColumnModel- Parameters:
newModel- the new selection model- Throws:
IllegalArgumentException- ifnewModelisnull- See Also:
getSelectionModel()
-
getSelectionModel
Returns theListSelectionModelthat is used to maintain column selection state.- Specified by:
getSelectionModelin interfaceTableColumnModel- Returns:
- the object that provides column selection state. Or
nullif 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:
setColumnSelectionAllowedin interfaceTableColumnModel- Parameters:
flag- true if column selection will be allowed, false otherwise- See Also:
TableColumnModel.getColumnSelectionAllowed()
-
getColumnSelectionAllowed
public boolean getColumnSelectionAllowed()Returns true if column selection is allowed, otherwise false. The default is false.- Specified by:
getColumnSelectionAllowedin interfaceTableColumnModel- Returns:
- the
columnSelectionAllowedproperty - See Also:
TableColumnModel.setColumnSelectionAllowed(boolean)
-
getSelectedColumns
public int[] getSelectedColumns()Returns an array of selected columns. IfselectionModelisnull, returns an empty array.- Specified by:
getSelectedColumnsin interfaceTableColumnModel- Returns:
- an array of selected columns or an empty array if nothing
is selected or the
selectionModelisnull
-
getSelectedColumnCount
public int getSelectedColumnCount()Returns the number of columns selected.- Specified by:
getSelectedColumnCountin interfaceTableColumnModel- Returns:
- the number of columns selected
-
addColumnModelListener
Adds a listener for table column model events.- Specified by:
addColumnModelListenerin interfaceTableColumnModel- Parameters:
x- aTableColumnModelListenerobject
-
removeColumnModelListener
Removes a listener for table column model events.- Specified by:
removeColumnModelListenerin interfaceTableColumnModel- Parameters:
x- aTableColumnModelListenerobject
-
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 - Since:
- 1.4
- See Also:
addColumnModelListener(javax.swing.event.TableColumnModelListener),removeColumnModelListener(javax.swing.event.TableColumnModelListener)
-
fireColumnAdded
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
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
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
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.- See Also:
EventListenerList
-
getListeners
Returns 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- iflistenerTypedoesn't specify a class or interface that implementsjava.util.EventListener- Since:
- 1.3
- See Also:
getColumnModelListeners()
-
propertyChange
Property Change Listener change method. Used to track changes to the column width or preferred column width.- Specified by:
propertyChangein interfacePropertyChangeListener- Parameters:
evt-PropertyChangeEvent
-
valueChanged
AListSelectionListenerthat forwardsListSelectionEventswhen there is a column selection change.- Specified by:
valueChangedin interfaceListSelectionListener- Parameters:
e- the change event
-
createSelectionModel
Creates a new default list selection model.- Returns:
- a newly created default list selection model.
-
recalcWidthCache
protected void recalcWidthCache()Recalculates the total combined width of all columns. Updates thetotalColumnWidthproperty.
-