APIdia Launch Partner
Top Description Fields Constructors Methods
javax.swing.table

public Class TableColumn

extends Object
implements Serializable
Class Inheritance
All Implemented Interfaces
java.io.Serializable
Annotations
@SuppressWarnings:serial
Imports
java.awt.Component, java.beans.BeanProperty, .PropertyChangeListener, java.io.Serializable, javax.swing.JLabel, .JTable, .UIManager, javax.swing.event.SwingPropertyChangeSupport

A TableColumn represents all the attributes of a column in a JTable, such as width, resizability, minimum and maximum width. In addition, the TableColumn provides slots for a renderer and an editor that can be used to display and edit the values in this column.

It is also possible to specify renderers and editors on a per type basis rather than a per column basis - see the setDefaultRenderer method in the JTable class. This default mechanism is only used when the renderer (or editor) in the TableColumn is null.

The TableColumn stores the link between the columns in the JTable and the columns in the TableModel. The modelIndex is the column in the TableModel, which will be queried for the data values for the cells in this column. As the column moves around in the view this modelIndex does not change.

Note

Some implementations may assume that all TableColumnModels are unique, therefore we would recommend that the same TableColumn instance not be added more than once to a TableColumnModel. To show TableColumns with the same column of data from the model, create a new instance with the same modelIndex.

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 java.beans.XMLEncoder.

Authors
Alan Chung, Philip Milne
See Also
javax.swing.table.TableColumnModel, javax.swing.table.DefaultTableColumnModel, javax.swing.table.JTableHeader#getDefaultRenderer(), JTable#getDefaultRenderer(Class), JTable#getDefaultEditor(Class), JTable#getCellRenderer(int, int), JTable#getCellEditor(int, int)

Field Summary

Modifier and TypeField and Description
public static final String
CELL_RENDERER_PROPERTY

Obsolete as of Java 2 platform v1.3.

protected TableCellEditor
cellEditor

The editor used to edit the data cells of the column.

protected TableCellRenderer
cellRenderer

The renderer used to draw the data cells of the column.

private SwingPropertyChangeSupport
changeSupport

If any PropertyChangeListeners have been registered, the changeSupport field describes them.

public static final String
COLUMN_WIDTH_PROPERTY

Obsolete as of Java 2 platform v1.3.

public static final String
HEADER_RENDERER_PROPERTY

Obsolete as of Java 2 platform v1.3.

public static final String
HEADER_VALUE_PROPERTY

Obsolete as of Java 2 platform v1.3.

protected TableCellRenderer
headerRenderer

The renderer used to draw the header of the column.

protected Object
headerValue

The header value of the column.

protected Object
identifier

This object is not used internally by the drawing machinery of the JTable; identifiers may be set in the TableColumn as an optional way to tag and locate table columns.

protected boolean
isResizable

If true, the user is allowed to resize the column; the default is true.

protected int
maxWidth

The maximum width of the column.

protected int
minWidth

The minimum width of the column.

protected int
modelIndex

The index of the column in the model which is to be displayed by this TableColumn.

private int
preferredWidth

The preferred width of the column.

protected transient int
resizedPostingDisableCount

Deprecated as of Java 2 platform v1.3
This field was not used in previous releases and there are currently no plans to support it in the future.
protected int
width

The width of the column.

Constructor Summary

AccessConstructor and Description
public
TableColumn()

Cover method, using a default model index of 0, default width of 75, a null renderer and a null editor.

public
TableColumn(int
the index of the column in the model that supplies the data for this column in the table; the model index remains the same even when columns are reordered in the view
modelIndex
)

Cover method, using a default width of 75, a null renderer and a null editor.

public
TableColumn(int
the index of the column in the model that supplies the data for this column in the table; the model index remains the same even when columns are reordered in the view
modelIndex
,
int
this column's preferred width and initial width
width
)

Cover method, using a null renderer and a null editor.

public
TableColumn(int
the index of the column in the model that supplies the data for this column in the table; the model index remains the same even when columns are reordered in the view
modelIndex
,
int
this column's preferred width and initial width
width
,
TableCellRenderer
the object used to render values in this column
cellRenderer
,
TableCellEditor
the object used to edit values in this column
cellEditor
)

Creates and initializes an instance of TableColumn with the specified model index, width, cell renderer, and cell editor; all TableColumn constructors delegate to this one.

Method Summary

Modifier and TypeMethod and Description
public synchronized void
addPropertyChangeListener(PropertyChangeListener
the listener to be added
listener
)

Adds a PropertyChangeListener to the listener list.

protected TableCellRenderer

Returns:

the default header renderer
createDefaultHeaderRenderer
()

As of Java 2 platform v1.3, this method is not called by the TableColumn constructor.

public void
disableResizedPosting()
Deprecated as of Java 2 platform v1.3

This field was not used in previous releases and there are currently no plans to support it in the future.

public void
enableResizedPosting()
Deprecated as of Java 2 platform v1.3

This field was not used in previous releases and there are currently no plans to support it in the future.

private void
firePropertyChange(String propertyName, Object oldValue, Object newValue)

private void
firePropertyChange(String propertyName, int oldValue, int newValue)

private void
firePropertyChange(String propertyName, boolean oldValue, boolean newValue)

public TableCellEditor

Returns:

the cellEditor property
getCellEditor
()

Returns the TableCellEditor used by the JTable to edit values for this column.

public TableCellRenderer

Returns:

the cellRenderer property
getCellRenderer
()

Returns the TableCellRenderer used by the JTable to draw values for this column.

public TableCellRenderer

Returns:

the headerRenderer property
getHeaderRenderer
()

Returns the TableCellRenderer used to draw the header of the TableColumn.

public Object

Returns:

the headerValue property
getHeaderValue
()

Returns the Object used as the value for the header renderer.

public Object

Returns:

the identifier property
getIdentifier
()

Returns the identifier object for this column.

public int

Returns:

the maxWidth property
getMaxWidth
()

Returns the maximum width for the TableColumn.

public int

Returns:

the minWidth property
getMinWidth
()

Returns the minimum width for the TableColumn.

public int

Returns:

the modelIndex property
getModelIndex
()

Returns the model index for this column.

public int

Returns:

the preferredWidth property
getPreferredWidth
()

Returns the preferred width of the TableColumn.

public synchronized PropertyChangeListener[]

Returns:

all of the PropertyChangeListeners added or an empty array if no listeners have been added
getPropertyChangeListeners
()

Returns an array of all the PropertyChangeListeners added to this TableColumn with addPropertyChangeListener().

public boolean

Returns:

the isResizable property
getResizable
()

Returns true if the user is allowed to resize the TableColumn's width, false otherwise.

public int

Returns:

the width property
getWidth
()

Returns the width of the TableColumn.

public synchronized void
removePropertyChangeListener(PropertyChangeListener
the listener to be removed
listener
)

Removes a PropertyChangeListener from the listener list.

public void
setCellEditor(TableCellEditor
the new cellEditor
cellEditor
)

Sets the editor to used by when a cell in this column is edited.

public void
setCellRenderer(TableCellRenderer
the new cellRenderer
cellRenderer
)

Sets the TableCellRenderer used by JTable to draw individual values for this column.

public void
setHeaderRenderer(TableCellRenderer
the new headerRenderer
headerRenderer
)

Sets the TableCellRenderer used to draw the TableColumn's header to headerRenderer.

public void
setHeaderValue(Object
the new headerValue
headerValue
)

Sets the Object whose string representation will be used as the value for the headerRenderer.

public void
setIdentifier(Object
an identifier for this column
identifier
)

Sets the TableColumn's identifier to anIdentifier.

public void
setMaxWidth(int
the new maximum width
maxWidth
)

Sets the TableColumn's maximum width to maxWidth or, if maxWidth is less than the minimum width, to the minimum width.

public void
setMinWidth(int
the new minimum width
minWidth
)

Sets the TableColumn's minimum width to minWidth, adjusting the new minimum width if necessary to ensure that 0 <= minWidth <= maxWidth.

public void
setModelIndex(int
the new modelIndex
modelIndex
)

Sets the model index for this column.

public void
setPreferredWidth(int
the new preferred width
preferredWidth
)

Sets this column's preferred width to preferredWidth.

public void
setResizable(boolean
if true, resizing is allowed; otherwise false
isResizable
)

Sets whether this column can be resized.

public void
setWidth(int
the new width
width
)

This method should not be used to set the widths of columns in the JTable, use setPreferredWidth instead.

public void
sizeWidthToFit()

Resizes the TableColumn to fit the width of its header cell.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

CELL_RENDERER_PROPERTYback to summary
public static final String CELL_RENDERER_PROPERTY

Obsolete as of Java 2 platform v1.3. Please use string literals to identify properties.

cellEditorback to summary
protected TableCellEditor cellEditor

The editor used to edit the data cells of the column.

cellRendererback to summary
protected TableCellRenderer cellRenderer

The renderer used to draw the data cells of the column.

changeSupportback to summary
private SwingPropertyChangeSupport changeSupport

If any PropertyChangeListeners have been registered, the changeSupport field describes them.

COLUMN_WIDTH_PROPERTYback to summary
public static final String COLUMN_WIDTH_PROPERTY

Obsolete as of Java 2 platform v1.3. Please use string literals to identify properties.

HEADER_RENDERER_PROPERTYback to summary
public static final String HEADER_RENDERER_PROPERTY

Obsolete as of Java 2 platform v1.3. Please use string literals to identify properties.

HEADER_VALUE_PROPERTYback to summary
public static final String HEADER_VALUE_PROPERTY

Obsolete as of Java 2 platform v1.3. Please use string literals to identify properties.

headerRendererback to summary
protected TableCellRenderer headerRenderer

The renderer used to draw the header of the column.

headerValueback to summary
protected Object headerValue

The header value of the column.

identifierback to summary
protected Object identifier

This object is not used internally by the drawing machinery of the JTable; identifiers may be set in the TableColumn as an optional way to tag and locate table columns. The table package does not modify or invoke any methods in these identifier objects other than the equals method which is used in the getColumnIndex() method in the DefaultTableColumnModel.

isResizableback to summary
protected boolean isResizable

If true, the user is allowed to resize the column; the default is true.

maxWidthback to summary
protected int maxWidth

The maximum width of the column.

minWidthback to summary
protected int minWidth

The minimum width of the column.

modelIndexback to summary
protected int modelIndex

The index of the column in the model which is to be displayed by this TableColumn. As columns are moved around in the view modelIndex remains constant.

preferredWidthback to summary
private int preferredWidth

The preferred width of the column.

resizedPostingDisableCountback to summary
protected transient int resizedPostingDisableCount

Deprecated

as of Java 2 platform v1.3

This field was not used in previous releases and there are currently no plans to support it in the future.

Annotations
@Deprecated
widthback to summary
protected int width

The width of the column.

Constructor Detail

TableColumnback to summary
public TableColumn()

Cover method, using a default model index of 0, default width of 75, a null renderer and a null editor. This method is intended for serialization.

See Also
TableColumn(int, int, TableCellRenderer, TableCellEditor)
TableColumnback to summary
public TableColumn(int modelIndex)

Cover method, using a default width of 75, a null renderer and a null editor.

Parameters
modelIndex:int

the index of the column in the model that supplies the data for this column in the table; the model index remains the same even when columns are reordered in the view

See Also
TableColumn(int, int, TableCellRenderer, TableCellEditor)
TableColumnback to summary
public TableColumn(int modelIndex, int width)

Cover method, using a null renderer and a null editor.

Parameters
modelIndex:int

the index of the column in the model that supplies the data for this column in the table; the model index remains the same even when columns are reordered in the view

width:int

this column's preferred width and initial width

See Also
TableColumn(int, int, TableCellRenderer, TableCellEditor)
TableColumnback to summary
public TableColumn(int modelIndex, int width, TableCellRenderer cellRenderer, TableCellEditor cellEditor)

Creates and initializes an instance of TableColumn with the specified model index, width, cell renderer, and cell editor; all TableColumn constructors delegate to this one. The value of width is used for both the initial and preferred width; if width is negative, they're set to 0. The minimum width is set to 15 unless the initial width is less, in which case the minimum width is set to the initial width.

When the cellRenderer or cellEditor parameter is null, a default value provided by the JTable getDefaultRenderer or getDefaultEditor method, respectively, is used to provide defaults based on the type of the data in this column. This column-centric rendering strategy can be circumvented by overriding the getCellRenderer methods in JTable.

Parameters
modelIndex:int

the index of the column in the model that supplies the data for this column in the table; the model index remains the same even when columns are reordered in the view

width:int

this column's preferred width and initial width

cellRenderer:TableCellRenderer

the object used to render values in this column

cellEditor:TableCellEditor

the object used to edit values in this column

See Also
getMinWidth(), JTable#getDefaultRenderer(Class), JTable#getDefaultEditor(Class), JTable#getCellRenderer(int, int), JTable#getCellEditor(int, int)

Method Detail

addPropertyChangeListenerback to summary
public synchronized void addPropertyChangeListener(PropertyChangeListener listener)

Adds a PropertyChangeListener to the listener list. The listener is registered for all bound properties of this class, including the following:

  • this TableColumn's modelIndex ("modelIndex")
  • this TableColumn's identifier ("identifier")
  • this TableColumn's header value ("headerValue")
  • this TableColumn's header renderer ("headerRenderer")
  • this TableColumn's cell renderer ("cellRenderer")
  • this TableColumn's cell editor ("cellEditor")
  • this TableColumn's width ("width")
  • this TableColumn's preferred width ("preferredWidth")
  • this TableColumn's minimum width ("minWidth")
  • this TableColumn's maximum width ("maxWidth")
  • this TableColumn's resizable state ("isResizable")
Parameters
listener:PropertyChangeListener

the listener to be added

See Also
removePropertyChangeListener(PropertyChangeListener)
createDefaultHeaderRendererback to summary
protected TableCellRenderer createDefaultHeaderRenderer()

As of Java 2 platform v1.3, this method is not called by the TableColumn constructor. Previously this method was used by the TableColumn to create a default header renderer. As of Java 2 platform v1.3, the default header renderer is null. JTableHeader now provides its own shared default renderer, just as the JTable does for its cell renderers.

Returns:TableCellRenderer

the default header renderer

See Also
javax.swing.table.JTableHeader#createDefaultRenderer()
disableResizedPostingback to summary
public void disableResizedPosting()

Deprecated

as of Java 2 platform v1.3

This field was not used in previous releases and there are currently no plans to support it in the future.

Annotations
@Deprecated
enableResizedPostingback to summary
public void enableResizedPosting()

Deprecated

as of Java 2 platform v1.3

This field was not used in previous releases and there are currently no plans to support it in the future.

Annotations
@Deprecated
firePropertyChangeback to summary
private void firePropertyChange(String propertyName, Object oldValue, Object newValue)
firePropertyChangeback to summary
private void firePropertyChange(String propertyName, int oldValue, int newValue)
firePropertyChangeback to summary
private void firePropertyChange(String propertyName, boolean oldValue, boolean newValue)
getCellEditorback to summary
public TableCellEditor getCellEditor()

Returns the TableCellEditor used by the JTable to edit values for this column. When the cellEditor is null, the JTable uses a default editor based on the class of the cells in that column. The default value for a cellEditor is null.

Returns:TableCellEditor

the cellEditor property

See Also
setCellEditor, JTable#setDefaultEditor
getCellRendererback to summary
public TableCellRenderer getCellRenderer()

Returns the TableCellRenderer used by the JTable to draw values for this column. The cellRenderer of the column not only controls the visual look for the column, but is also used to interpret the value object supplied by the TableModel. When the cellRenderer is null, the JTable uses a default renderer based on the class of the cells in that column. The default value for a cellRenderer is null.

Returns:TableCellRenderer

the cellRenderer property

See Also
setCellRenderer, JTable#setDefaultRenderer
getHeaderRendererback to summary
public TableCellRenderer getHeaderRenderer()

Returns the TableCellRenderer used to draw the header of the TableColumn. When the headerRenderer is null, the JTableHeader uses its defaultRenderer. The default value for a headerRenderer is null.

Returns:TableCellRenderer

the headerRenderer property

See Also
setHeaderRenderer, setHeaderValue, javax.swing.table.JTableHeader#getDefaultRenderer()
getHeaderValueback to summary
public Object getHeaderValue()

Returns the Object used as the value for the header renderer.

Returns:Object

the headerValue property

See Also
setHeaderValue
getIdentifierback to summary
public Object getIdentifier()

Returns the identifier object for this column. Note identifiers are not used by JTable, they are purely a convenience for external use. If the identifier is null, getIdentifier() returns getHeaderValue as a default.

Returns:Object

the identifier property

See Also
setIdentifier
getMaxWidthback to summary
public int getMaxWidth()

Returns the maximum width for the TableColumn. The TableColumn's width can't be made larger than this either by the user or programmatically. The default maxWidth is Integer.MAX_VALUE.

Returns:int

the maxWidth property

See Also
setMaxWidth
getMinWidthback to summary
public int getMinWidth()

Returns the minimum width for the TableColumn. The TableColumn's width can't be made less than this either by the user or programmatically.

Returns:int

the minWidth property

See Also
setMinWidth, TableColumn(int, int, TableCellRenderer, TableCellEditor)
getModelIndexback to summary
public int getModelIndex()

Returns the model index for this column.

Returns:int

the modelIndex property

getPreferredWidthback to summary
public int getPreferredWidth()

Returns the preferred width of the TableColumn. The default preferred width is 75.

Returns:int

the preferredWidth property

See Also
setPreferredWidth
getPropertyChangeListenersback to summary
public synchronized PropertyChangeListener[] getPropertyChangeListeners()

Returns an array of all the PropertyChangeListeners added to this TableColumn with addPropertyChangeListener().

Returns:PropertyChangeListener[]

all of the PropertyChangeListeners added or an empty array if no listeners have been added

Since
1.4
getResizableback to summary
public boolean getResizable()

Returns true if the user is allowed to resize the TableColumn's width, false otherwise. You can change the width programmatically regardless of this setting. The default is true.

Returns:boolean

the isResizable property

See Also
setResizable
getWidthback to summary
public int getWidth()

Returns the width of the TableColumn. The default width is 75.

Returns:int

the width property

See Also
setWidth
removePropertyChangeListenerback to summary
public synchronized void removePropertyChangeListener(PropertyChangeListener listener)

Removes a PropertyChangeListener from the listener list. The PropertyChangeListener to be removed was registered for all properties.

Parameters
listener:PropertyChangeListener

the listener to be removed

setCellEditorback to summary
public void setCellEditor(TableCellEditor cellEditor)

Sets the editor to used by when a cell in this column is edited.

Parameters
cellEditor:TableCellEditor

the new cellEditor

Annotations
@BeanProperty
description:The editor to use for cell values.
See Also
getCellEditor
setCellRendererback to summary
public void setCellRenderer(TableCellRenderer cellRenderer)

Sets the TableCellRenderer used by JTable to draw individual values for this column.

Parameters
cellRenderer:TableCellRenderer

the new cellRenderer

Annotations
@BeanProperty
description:The renderer to use for cell values.
See Also
getCellRenderer
setHeaderRendererback to summary
public void setHeaderRenderer(TableCellRenderer headerRenderer)

Sets the TableCellRenderer used to draw the TableColumn's header to headerRenderer.

It is the header renderers responsibility to render the sorting indicator. If you are using sorting and specify a renderer your renderer must render the sorting indication.

Parameters
headerRenderer:TableCellRenderer

the new headerRenderer

Annotations
@BeanProperty
description:The header renderer.
See Also
getHeaderRenderer
setHeaderValueback to summary
public void setHeaderValue(Object headerValue)

Sets the Object whose string representation will be used as the value for the headerRenderer. When the TableColumn is created, the default headerValue is null.

Parameters
headerValue:Object

the new headerValue

Annotations
@BeanProperty
description:The text to be used by the header renderer.
See Also
getHeaderValue
setIdentifierback to summary
public void setIdentifier(Object identifier)

Sets the TableColumn's identifier to anIdentifier.

Note

identifiers are not used by the JTable, they are purely a convenience for the external tagging and location of columns.

Parameters
identifier:Object

an identifier for this column

Annotations
@BeanProperty
description:A unique identifier for this column.
See Also
getIdentifier
setMaxWidthback to summary
public void setMaxWidth(int maxWidth)

Sets the TableColumn's maximum width to maxWidth or, if maxWidth is less than the minimum width, to the minimum width.

If the value of the width or preferredWidth property is more than the new maximum width, this method sets that property to the new maximum width.

Parameters
maxWidth:int

the new maximum width

Annotations
@BeanProperty
description:The maximum width of the column.
See Also
getMaxWidth, setPreferredWidth, setMinWidth
setMinWidthback to summary
public void setMinWidth(int minWidth)

Sets the TableColumn's minimum width to minWidth, adjusting the new minimum width if necessary to ensure that 0 <= minWidth <= maxWidth. For example, if the minWidth argument is negative, this method sets the minWidth property to 0.

If the value of the width or preferredWidth property is less than the new minimum width, this method sets that property to the new minimum width.

Parameters
minWidth:int

the new minimum width

Annotations
@BeanProperty
description:The minimum width of the column.
See Also
getMinWidth, setPreferredWidth, setMaxWidth
setModelIndexback to summary
public void setModelIndex(int modelIndex)

Sets the model index for this column. The model index is the index of the column in the model that will be displayed by this TableColumn. As the TableColumn is moved around in the view the model index remains constant.

Parameters
modelIndex:int

the new modelIndex

Annotations
@BeanProperty
description:The model index.
setPreferredWidthback to summary
public void setPreferredWidth(int preferredWidth)

Sets this column's preferred width to preferredWidth. If preferredWidth exceeds the minimum or maximum width, it is adjusted to the appropriate limiting value.

For details on how the widths of columns in the JTable (and JTableHeader) are calculated from the preferredWidth, see the doLayout method in JTable.

Parameters
preferredWidth:int

the new preferred width

Annotations
@BeanProperty
description:The preferred width of the column.
See Also
getPreferredWidth, JTable#doLayout()
setResizableback to summary
public void setResizable(boolean isResizable)

Sets whether this column can be resized.

Parameters
isResizable:boolean

if true, resizing is allowed; otherwise false

Annotations
@BeanProperty
description:Whether or not this column can be resized.
See Also
getResizable
setWidthback to summary
public void setWidth(int width)

This method should not be used to set the widths of columns in the JTable, use setPreferredWidth instead. Like a layout manager in the AWT, the JTable adjusts a column's width automatically whenever the table itself changes size, or a column's preferred width is changed. Setting widths programmatically therefore has no long term effect.

This method sets this column's width to width. If width exceeds the minimum or maximum width, it is adjusted to the appropriate limiting value.

Parameters
width:int

the new width

Annotations
@BeanProperty
description:The width of the column.
See Also
getWidth, setMinWidth, setMaxWidth, setPreferredWidth, JTable#doLayout()
sizeWidthToFitback to summary
public void sizeWidthToFit()

Resizes the TableColumn to fit the width of its header cell. This method does nothing if the header renderer is null (the default case). Otherwise, it sets the minimum, maximum and preferred widths of this column to the widths of the minimum, maximum and preferred sizes of the Component delivered by the header renderer. The transient "width" property of this TableColumn is also set to the preferred width. Note this method is not used internally by the table package.

See Also
setPreferredWidth