DefaultTableColumnModel (Java Platform SE 6) (original) (raw)
javax.swing.table
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 JavaBeansTM has been added to the java.beans
package. Please see XMLEncoder.
See Also:
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<TableColumn> | 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 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 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. |
TableColumnModelListener[] | getColumnModelListeners() Returns an array of all the column model listeners registered on this model. |
Enumeration<TableColumn> | getColumns() Returns an Enumeration of all the columns in the model. |
boolean | getColumnSelectionAllowed() Returns true if column selection is allowed, otherwise false. |
<T extends EventListener> T[] | getListeners(Class listenerType) Returns an array of all the objects currently registered as _Foo_Listeners upon 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](../../../javax/swing/table/DefaultTableColumnModel.html#moveColumn%28int, int%29)(int columnIndex, int newIndex) Moves the column and heading at columnIndex 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 the column from thetableColumns 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 forwardsListSelectionEvents 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](../../../java/lang/Object.html#wait%28long, int%29) |
Field Detail |
---|
tableColumns
protected Vector<TableColumn> 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 thetableColumns
array. This method also posts the columnAdded
event to its listeners.
Specified by:
[addColumn](../../../javax/swing/table/TableColumnModel.html#addColumn%28javax.swing.table.TableColumn%29)
in interface [TableColumnModel](../../../javax/swing/table/TableColumnModel.html "interface in javax.swing.table")
Parameters:
aColumn
- the TableColumn
to be added
Throws:
[IllegalArgumentException](../../../java/lang/IllegalArgumentException.html "class in java.lang")
- if aColumn
isnull
See Also:
removeColumn(javax.swing.table.TableColumn)
removeColumn
public void removeColumn(TableColumn column)
Deletes the column
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 a columnRemoved
event to its listeners.
Specified by:
[removeColumn](../../../javax/swing/table/TableColumnModel.html#removeColumn%28javax.swing.table.TableColumn%29)
in interface [TableColumnModel](../../../javax/swing/table/TableColumnModel.html "interface in javax.swing.table")
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
tonewIndex
. 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 ifcolumnIndex
equals newIndex
. This method also posts a columnMoved
event to its listeners.
Specified by:
[moveColumn](../../../javax/swing/table/TableColumnModel.html#moveColumn%28int, int%29)
in interface [TableColumnModel](../../../javax/swing/table/TableColumnModel.html "interface in javax.swing.table")
Parameters:
columnIndex
- the index of column to be moved
newIndex
- new index to move the column
Throws:
[IllegalArgumentException](../../../java/lang/IllegalArgumentException.html "class in java.lang")
- if column
ornewIndex
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](../../../javax/swing/table/TableColumnModel.html#setColumnMargin%28int%29)
in interface [TableColumnModel](../../../javax/swing/table/TableColumnModel.html "interface in javax.swing.table")
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](../../../javax/swing/table/TableColumnModel.html#getColumnCount%28%29)
in interface [TableColumnModel](../../../javax/swing/table/TableColumnModel.html "interface in javax.swing.table")
Returns:
the number of columns in the tableColumns
array
See Also:
getColumns
public Enumeration<TableColumn> getColumns()
Returns an Enumeration
of all the columns in the model.
Specified by:
[getColumns](../../../javax/swing/table/TableColumnModel.html#getColumns%28%29)
in interface [TableColumnModel](../../../javax/swing/table/TableColumnModel.html "interface in javax.swing.table")
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](../../../javax/swing/table/TableColumnModel.html#getColumnIndex%28java.lang.Object%29)
in interface [TableColumnModel](../../../javax/swing/table/TableColumnModel.html "interface in javax.swing.table")
Parameters:
identifier
- the identifier object
Returns:
the index of the first column in the tableColumns
array whose identifier is equal to identifier
Throws:
[IllegalArgumentException](../../../java/lang/IllegalArgumentException.html "class in java.lang")
- if identifier
is null
, or if noTableColumn
has thisidentifier
See Also:
getColumn
public TableColumn getColumn(int columnIndex)
Returns the TableColumn
object for the column at columnIndex
.
Specified by:
[getColumn](../../../javax/swing/table/TableColumnModel.html#getColumn%28int%29)
in interface [TableColumnModel](../../../javax/swing/table/TableColumnModel.html "interface in javax.swing.table")
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](../../../javax/swing/table/TableColumnModel.html#getColumnMargin%28%29)
in interface [TableColumnModel](../../../javax/swing/table/TableColumnModel.html "interface in javax.swing.table")
Returns:
the maximum width for the TableColumn
See Also:
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. 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 accomodate 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 given xPosition
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](../../../javax/swing/table/TableColumnModel.html#getColumnIndexAtX%28int%29)
in interface [TableColumnModel](../../../javax/swing/table/TableColumnModel.html "interface in javax.swing.table")
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](../../../javax/swing/table/TableColumnModel.html#getTotalColumnWidth%28%29)
in interface [TableColumnModel](../../../javax/swing/table/TableColumnModel.html "interface in javax.swing.table")
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](../../../javax/swing/table/TableColumnModel.html#setSelectionModel%28javax.swing.ListSelectionModel%29)
in interface [TableColumnModel](../../../javax/swing/table/TableColumnModel.html "interface in javax.swing.table")
Parameters:
newModel
- the new selection model
Throws:
[IllegalArgumentException](../../../java/lang/IllegalArgumentException.html "class in java.lang")
- if newModel
is null
See Also:
getSelectionModel
public ListSelectionModel getSelectionModel()
Returns the ListSelectionModel
that is used to maintain column selection state.
Specified by:
[getSelectionModel](../../../javax/swing/table/TableColumnModel.html#getSelectionModel%28%29)
in interface [TableColumnModel](../../../javax/swing/table/TableColumnModel.html "interface in javax.swing.table")
Returns:
the object that provides column selection state. Ornull
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](../../../javax/swing/table/TableColumnModel.html#setColumnSelectionAllowed%28boolean%29)
in interface [TableColumnModel](../../../javax/swing/table/TableColumnModel.html "interface in javax.swing.table")
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](../../../javax/swing/table/TableColumnModel.html#getColumnSelectionAllowed%28%29)
in interface [TableColumnModel](../../../javax/swing/table/TableColumnModel.html "interface in javax.swing.table")
Returns:
the columnSelectionAllowed
property
See Also:
TableColumnModel.setColumnSelectionAllowed(boolean)
getSelectedColumns
public int[] getSelectedColumns()
Returns an array of selected columns. If selectionModel
is null
, returns an empty array.
Specified by:
[getSelectedColumns](../../../javax/swing/table/TableColumnModel.html#getSelectedColumns%28%29)
in interface [TableColumnModel](../../../javax/swing/table/TableColumnModel.html "interface in javax.swing.table")
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](../../../javax/swing/table/TableColumnModel.html#getSelectedColumnCount%28%29)
in interface [TableColumnModel](../../../javax/swing/table/TableColumnModel.html "interface in javax.swing.table")
Returns:
the number of columns selected
addColumnModelListener
public void addColumnModelListener(TableColumnModelListener x)
Adds a listener for table column model events.
Specified by:
[addColumnModelListener](../../../javax/swing/table/TableColumnModel.html#addColumnModelListener%28javax.swing.event.TableColumnModelListener%29)
in interface [TableColumnModel](../../../javax/swing/table/TableColumnModel.html "interface in javax.swing.table")
Parameters:
x
- a TableColumnModelListener
object
removeColumnModelListener
public void removeColumnModelListener(TableColumnModelListener x)
Removes a listener for table column model events.
Specified by:
[removeColumnModelListener](../../../javax/swing/table/TableColumnModel.html#removeColumnModelListener%28javax.swing.event.TableColumnModelListener%29)
in interface [TableColumnModel](../../../javax/swing/table/TableColumnModel.html "interface in javax.swing.table")
Parameters:
x
- a TableColumnModelListener
object
getColumnModelListeners
public TableColumnModelListener[] 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
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:
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:
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:
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:
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
public <T extends EventListener> T[] getListeners(Class listenerType)
Returns an array of all the objects currently registered as _Foo_Listener
s upon this model._Foo_Listener
s are registered using theadd_Foo_Listener
method.
You can specify the listenerType
argument with a class literal, such as_Foo_Listener.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.
Parameters:
listenerType
- the type of listeners requested; this parameter should specify an interface that descends fromjava.util.EventListener
Returns:
an array of all objects registered as_Foo_Listener
s on this model, or an empty array if no such listeners have been added
Throws:
[ClassCastException](../../../java/lang/ClassCastException.html "class in java.lang")
- if listenerType
doesn't specify a class or interface that implementsjava.util.EventListener
Since:
1.3
See Also:
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](../../../java/beans/PropertyChangeListener.html#propertyChange%28java.beans.PropertyChangeEvent%29)
in interface [PropertyChangeListener](../../../java/beans/PropertyChangeListener.html "interface in java.beans")
Parameters:
evt
- PropertyChangeEvent
valueChanged
public void valueChanged(ListSelectionEvent e)
A ListSelectionListener
that forwardsListSelectionEvents
when there is a column selection change.
Specified by:
[valueChanged](../../../javax/swing/event/ListSelectionListener.html#valueChanged%28javax.swing.event.ListSelectionEvent%29)
in interface [ListSelectionListener](../../../javax/swing/event/ListSelectionListener.html "interface in javax.swing.event")
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 thetotalColumnWidth
property.
Submit a bug or feature
For further API reference and developer documentation, see Java SE Developer Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Copyright © 1993, 2015, Oracle and/or its affiliates. All rights reserved. Use is subject to license terms. Also see the documentation redistribution policy.
Scripting on this page tracks web page traffic, but does not change the content in any way.