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 ChangeEvent
changeEvent
Change event (only one needed)protected int
columnMargin
Width margin between each columnprotected boolean
columnSelectionAllowed
Column selection allowed in this column modelprotected EventListenerList
listenerList
List of TableColumnModelListenerprotected ListSelectionModel
selectionModel
Model for keeping track of column selectionsprotected Vector<TableColumn>
tableColumns
Array of TableColumn objects in this modelprotected int
totalColumnWidth
A 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 void
addColumn(TableColumn aColumn)
AppendsaColumn
to the end of thetableColumns
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 theTableColumn
object for the column atcolumnIndex
.int
getColumnCount()
Returns the number of columns in thetableColumns
array.int
getColumnIndex(Object identifier)
Returns the index of the first column in thetableColumns
array whose identifier is equal toidentifier
, when compared usingequals
.int
getColumnIndexAtX(int x)
Returns the index of the column that lies at positionx
, or -1 if no column covers this point.int
getColumnMargin()
Returns the width margin forTableColumn
.TableColumnModelListener[]
getColumnModelListeners()
Returns an array of all the column model listeners registered on this model.Enumeration<TableColumn>
getColumns()
Returns anEnumeration
of all the columns in the model.boolean
getColumnSelectionAllowed()
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 asFooListener
s upon this model.int
getSelectedColumnCount()
Returns the number of columns selected.int[]
getSelectedColumns()
Returns an array of selected columns.ListSelectionModel
getSelectionModel()
Returns theListSelectionModel
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 atcolumnIndex
tonewIndex
.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 thecolumn
from thetableColumns
array.void
removeColumnModelListener(TableColumnModelListener x)
Removes a listener for table column model events.void
setColumnMargin(int newMargin)
Sets the column margin tonewMargin
.void
setColumnSelectionAllowed(boolean flag)
Sets whether column selection is allowed.void
setSelectionModel(ListSelectionModel newModel)
Sets the selection model for thisTableColumnModel
tonewModel
and registers for listener notifications from the new selection model.void
valueChanged(ListSelectionEvent e)
AListSelectionListener
that forwardsListSelectionEvents
when 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
AppendsaColumn
to the end of thetableColumns
array. This method also posts thecolumnAdded
event to its listeners.- Specified by:
addColumn
in interfaceTableColumnModel
- Parameters:
aColumn
- theTableColumn
to be added- Throws:
IllegalArgumentException
- ifaColumn
isnull
- See Also:
removeColumn(javax.swing.table.TableColumn)
-
removeColumn
Deletes thecolumn
from thetableColumns
array. This method will do nothing ifcolumn
is not in the table's columns list.tile
is called to resize both the header and table views. This method also posts acolumnRemoved
event to its listeners.- Specified by:
removeColumn
in interfaceTableColumnModel
- Parameters:
column
- theTableColumn
to be removed- See Also:
addColumn(javax.swing.table.TableColumn)
-
moveColumn
public void moveColumn(int columnIndex, int newIndex)Moves the column and heading atcolumnIndex
tonewIndex
. The old column atcolumnIndex
will now be found atnewIndex
. The column that used to be atnewIndex
is shifted left or right to make room. This will not move any columns ifcolumnIndex
equalsnewIndex
. This method also posts acolumnMoved
event to its listeners.- Specified by:
moveColumn
in interfaceTableColumnModel
- Parameters:
columnIndex
- the index of column to be movednewIndex
- new index to move the column- Throws:
IllegalArgumentException
- ifcolumn
ornewIndex
are not in the valid range
-
setColumnMargin
public void setColumnMargin(int newMargin)Sets the column margin tonewMargin
. This method also posts acolumnMarginChanged
event to its listeners.- Specified by:
setColumnMargin
in interfaceTableColumnModel
- Parameters:
newMargin
- the new margin width, in pixels- See Also:
getColumnMargin()
,getTotalColumnWidth()
-
getColumnCount
public int getColumnCount()Returns the number of columns in thetableColumns
array.- Specified by:
getColumnCount
in interfaceTableColumnModel
- Returns:
- the number of columns in the
tableColumns
array - See Also:
getColumns()
-
getColumns
Returns anEnumeration
of all the columns in the model.- Specified by:
getColumns
in interfaceTableColumnModel
- Returns:
- an
Enumeration
of the columns in the model
-
getColumnIndex
Returns the index of the first column in thetableColumns
array whose identifier is equal toidentifier
, when compared usingequals
.- Specified by:
getColumnIndex
in interfaceTableColumnModel
- Parameters:
identifier
- the identifier object- Returns:
- the index of the first column in the
tableColumns
array whose identifier is equal toidentifier
- Throws:
IllegalArgumentException
- ifidentifier
isnull
, or if noTableColumn
has thisidentifier
- See Also:
getColumn(int)
-
getColumn
Returns theTableColumn
object for the column atcolumnIndex
.- Specified by:
getColumn
in interfaceTableColumnModel
- Parameters:
columnIndex
- the index of the column desired- Returns:
- the
TableColumn
object for the column atcolumnIndex
-
getColumnMargin
public int getColumnMargin()Returns the width margin forTableColumn
. The defaultcolumnMargin
is 1.- Specified by:
getColumnMargin
in 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 givenxPosition
should 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.columnAtPoint
can be used instead.- Specified by:
getColumnIndexAtX
in 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:
getTotalColumnWidth
in interfaceTableColumnModel
- Returns:
- the
totalColumnWidth
property
-
setSelectionModel
Sets the selection model for thisTableColumnModel
tonewModel
and registers for listener notifications from the new selection model. IfnewModel
isnull
, an exception is thrown.- Specified by:
setSelectionModel
in interfaceTableColumnModel
- Parameters:
newModel
- the new selection model- Throws:
IllegalArgumentException
- ifnewModel
isnull
- See Also:
getSelectionModel()
-
getSelectionModel
Returns theListSelectionModel
that is used to maintain column selection state.- Specified by:
getSelectionModel
in interfaceTableColumnModel
- 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 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:
getColumnSelectionAllowed
in interfaceTableColumnModel
- Returns:
- the
columnSelectionAllowed
property - See Also:
TableColumnModel.setColumnSelectionAllowed(boolean)
-
getSelectedColumns
public int[] getSelectedColumns()Returns an array of selected columns. IfselectionModel
isnull
, returns an empty array.- Specified by:
getSelectedColumns
in interfaceTableColumnModel
- Returns:
- an array of selected columns or an empty array if nothing
is selected or the
selectionModel
isnull
-
getSelectedColumnCount
public int getSelectedColumnCount()Returns the number of columns selected.- Specified by:
getSelectedColumnCount
in interfaceTableColumnModel
- Returns:
- the number of columns selected
-
addColumnModelListener
Adds a listener for table column model events.- Specified by:
addColumnModelListener
in interfaceTableColumnModel
- Parameters:
x
- aTableColumnModelListener
object
-
removeColumnModelListener
Removes a listener for table column model events.- Specified by:
removeColumnModelListener
in interfaceTableColumnModel
- Parameters:
x
- aTableColumnModelListener
object
-
getColumnModelListeners
Returns an array of all the column model listeners registered on this model.- Returns:
- all of this default table column model's
ColumnModelListener
s 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 asFooListener
s upon this model.FooListener
s are registered using theaddFooListener
method.You can specify the
listenerType
argument with a class literal, such asFooListener.class
. For example, you can query aDefaultTableColumnModel
m
for 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
FooListener
s on this model, or an empty array if no such listeners have been added - Throws:
ClassCastException
- iflistenerType
doesn'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:
propertyChange
in interfacePropertyChangeListener
- Parameters:
evt
-PropertyChangeEvent
-
valueChanged
AListSelectionListener
that forwardsListSelectionEvents
when there is a column selection change.- Specified by:
valueChanged
in 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 thetotalColumnWidth
property.
-