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 Summary
Modifier and TypeFieldDescriptionprotected ChangeEvent
Change event (only one needed)protected int
Width margin between each columnprotected boolean
Column selection allowed in this column modelprotected EventListenerList
List of TableColumnModelListenerprotected ListSelectionModel
Model for keeping track of column selectionsprotected Vector
<TableColumn> Array of TableColumn objects in this modelprotected int
A local cache of the combined width of all columns -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
addColumn
(TableColumn aColumn) AppendsaColumn
to the end of thetableColumns
array.void
Adds a listener for table column model events.protected ListSelectionModel
Creates a new default list selection model.protected void
Notifies all listeners that have registered interest for notification on this event type.protected void
Notifies all listeners that have registered interest for notification on this event type.protected void
Notifies all listeners that have registered interest for notification on this event type.protected void
Notifies all listeners that have registered interest for notification on this event type.protected void
Notifies all listeners that have registered interest for notification on this event type.getColumn
(int columnIndex) Returns theTableColumn
object for the column atcolumnIndex
.int
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
Returns the width margin forTableColumn
.Returns an array of all the column model listeners registered on this model.Returns anEnumeration
of all the columns in the model.boolean
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
Returns the number of columns selected.int[]
Returns an array of selected columns.Returns theListSelectionModel
that is used to maintain column selection state.int
Returns the total combined width of all columns.void
moveColumn
(int columnIndex, int newIndex) Moves the column and heading atcolumnIndex
tonewIndex
.void
Property Change Listener change method.protected void
Recalculates the total combined width of all columns.void
removeColumn
(TableColumn column) Deletes thecolumn
from thetableColumns
array.void
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
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
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:
-
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:
-
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
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
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
- Throws:
ArrayIndexOutOfBoundsException
- ifcolumnIndex
is out of range: (columnIndex < 0 || columnIndex >= getColumnCount()
)
-
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:
-
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:
-
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
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:
-
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:
-
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:
-
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:
-
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:
-
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:
-
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:
-
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:
-
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:
-
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:
-
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.
-