TableColumn (Java SE 15 & JDK 15) (original) (raw)
All Implemented Interfaces:
[Serializable](../../../../java.base/java/io/Serializable.html "interface in java.io")
public class TableColumn extends Object implements Serializable
A TableColumn
represents all the attributes of a column in aJTable
, 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 thesetDefaultRenderer
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 theJTable
and the columns in the TableModel
. The modelIndex
is the column in theTableModel
, which will be queried for the data values for the cells in this column. As the column moves around in the view thismodelIndex
does not change.
Note: Some implementations may assume that allTableColumnModel
s are unique, therefore we would recommend that the same TableColumn
instance not be added more than once to a TableColumnModel
. To show TableColumn
s with the same column of data from the model, create a new instance with the samemodelIndex
.
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:
TableColumnModel, DefaultTableColumnModel, JTableHeader.getDefaultRenderer(), JTable.getDefaultRenderer(Class), JTable.getDefaultEditor(Class), JTable.getCellRenderer(int, int), JTable.getCellEditor(int, int)
Field Summary
Fields
Modifier and Type | Field | Description |
---|---|---|
static 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. |
static String | COLUMN_WIDTH_PROPERTY | Obsolete as of Java 2 platform v1.3. |
static String | HEADER_RENDERER_PROPERTY | Obsolete as of Java 2 platform v1.3. |
static 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 theTableColumn 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. |
protected int | resizedPostingDisableCount | Deprecated. |
protected int | width | The width of the column. |
Constructor Summary
Constructors
Constructor | Description |
---|---|
TableColumn() | Cover method, using a default model index of 0, default width of 75, a null renderer and anull editor. |
TableColumn(int modelIndex) | Cover method, using a default width of 75, a null renderer and a null editor. |
TableColumn(int modelIndex, int width) | Cover method, using a null renderer and anull editor. |
TableColumn(int modelIndex, int width,TableCellRenderer cellRenderer,TableCellEditor cellEditor) | Creates and initializes an instance ofTableColumn with the specified model index, width, cell renderer, and cell editor; all TableColumn constructors delegate to this one. |
Method Summary
Modifier and Type | Method | Description |
---|---|---|
void | addPropertyChangeListener(PropertyChangeListener listener) | Adds a PropertyChangeListener to the listener list. |
protected TableCellRenderer | createDefaultHeaderRenderer() | As of Java 2 platform v1.3, this method is not called by the TableColumn constructor. |
void | disableResizedPosting() | Deprecated. |
void | enableResizedPosting() | Deprecated. |
TableCellEditor | getCellEditor() | Returns the TableCellEditor used by theJTable to edit values for this column. |
TableCellRenderer | getCellRenderer() | Returns the TableCellRenderer used by theJTable to draw values for this column. |
TableCellRenderer | getHeaderRenderer() | Returns the TableCellRenderer used to draw the header of theTableColumn. |
Object | getHeaderValue() | Returns the Object used as the value for the header renderer. |
Object | getIdentifier() | Returns the identifier object for this column. |
int | getMaxWidth() | Returns the maximum width for the TableColumn. |
int | getMinWidth() | Returns the minimum width for the TableColumn. |
int | getModelIndex() | Returns the model index for this column. |
int | getPreferredWidth() | Returns the preferred width of the TableColumn. |
PropertyChangeListener[] | getPropertyChangeListeners() | Returns an array of all the PropertyChangeListeners added to this TableColumn with addPropertyChangeListener(). |
boolean | getResizable() | Returns true if the user is allowed to resize theTableColumn's width, false otherwise. |
int | getWidth() | Returns the width of the TableColumn. |
void | removePropertyChangeListener(PropertyChangeListener listener) | Removes a PropertyChangeListener from the listener list. |
void | setCellEditor(TableCellEditor cellEditor) | Sets the editor to used by when a cell in this column is edited. |
void | setCellRenderer(TableCellRenderer cellRenderer) | Sets the TableCellRenderer used by JTable to draw individual values for this column. |
void | setHeaderRenderer(TableCellRenderer headerRenderer) | Sets the TableCellRenderer used to draw theTableColumn's header to headerRenderer. |
void | setHeaderValue(Object headerValue) | Sets the Object whose string representation will be used as the value for the headerRenderer. |
void | setIdentifier(Object identifier) | Sets the TableColumn's identifier toanIdentifier. |
void | setMaxWidth(int maxWidth) | Sets the TableColumn's maximum width tomaxWidth or, if maxWidth is less than the minimum width, to the minimum width. |
void | setMinWidth(int minWidth) | Sets the TableColumn's minimum width tominWidth, adjusting the new minimum width if necessary to ensure that 0 <= minWidth <= maxWidth. |
void | setModelIndex(int modelIndex) | Sets the model index for this column. |
void | setPreferredWidth(int preferredWidth) | Sets this column's preferred width to preferredWidth. |
void | setResizable(boolean isResizable) | Sets whether this column can be resized. |
void | setWidth(int width) | This method should not be used to set the widths of columns in theJTable, use setPreferredWidth instead. |
void | sizeWidthToFit() | Resizes the TableColumn to fit the width of its header cell. |
Methods declared in class java.lang.Object
[clone](../../../../java.base/java/lang/Object.html#clone%28%29), [equals](../../../../java.base/java/lang/Object.html#equals%28java.lang.Object%29), [finalize](../../../../java.base/java/lang/Object.html#finalize%28%29), [getClass](../../../../java.base/java/lang/Object.html#getClass%28%29), [hashCode](../../../../java.base/java/lang/Object.html#hashCode%28%29), [notify](../../../../java.base/java/lang/Object.html#notify%28%29), [notifyAll](../../../../java.base/java/lang/Object.html#notifyAll%28%29), [toString](../../../../java.base/java/lang/Object.html#toString%28%29), [wait](../../../../java.base/java/lang/Object.html#wait%28%29), [wait](../../../../java.base/java/lang/Object.html#wait%28long%29), [wait](../../../../java.base/java/lang/Object.html#wait%28long,int%29)
Field Details
COLUMN_WIDTH_PROPERTY
public static final String COLUMN_WIDTH_PROPERTY
Obsolete as of Java 2 platform v1.3. Please use string literals to identify properties.
See Also:
Constant Field ValuesCELL_RENDERER_PROPERTY
public static final String CELL_RENDERER_PROPERTY
Obsolete as of Java 2 platform v1.3. Please use string literals to identify properties.
See Also:
Constant Field ValuesmodelIndex
protected int modelIndex
The index of the column in the model which is to be displayed by thisTableColumn
. As columns are moved around in the viewmodelIndex
remains constant.identifier
protected Object identifier
This object is not used internally by the drawing machinery of theJTable
; identifiers may be set in theTableColumn
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 theequals
method which is used in thegetColumnIndex()
method in theDefaultTableColumnModel
.width
protected int width
The width of the column.minWidth
protected int minWidth
The minimum width of the column.maxWidth
protected int maxWidth
The maximum width of the column.cellRenderer
The renderer used to draw the data cells of the column.
cellEditor
The editor used to edit the data cells of the column.
isResizable
protected boolean isResizable
If true, the user is allowed to resize the column; the default is true.resizedPostingDisableCount
@Deprecated protected transient int resizedPostingDisableCount
This field was not used in previous releases and there are currently no plans to support it in the future.Constructor Details
TableColumn
public TableColumn()
Cover method, using a default model index of 0, default width of 75, anull
renderer and anull
editor. This method is intended for serialization.
See Also:
TableColumn(int, int, TableCellRenderer, TableCellEditor)TableColumn
public TableColumn(int modelIndex)
Cover method, using a default width of 75, anull
renderer and anull
editor.
Parameters:
modelIndex
- 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)TableColumn
public TableColumn(int modelIndex, int width)
Cover method, using anull
renderer and anull
editor.
Parameters:
modelIndex
- 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
- this column's preferred width and initial width
See Also:
TableColumn(int, int, TableCellRenderer, TableCellEditor)TableColumn
Creates and initializes an instance of
TableColumn
with the specified model index, width, cell renderer, and cell editor; allTableColumn
constructors delegate to this one. The value ofwidth
is used for both the initial and preferred width; ifwidth
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 thecellRenderer
orcellEditor
parameter isnull
, a default value provided by theJTable
getDefaultRenderer
orgetDefaultEditor
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 thegetCellRenderer
methods inJTable
.
Parameters:
modelIndex
- 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
- this column's preferred width and initial width
cellRenderer
- the object used to render values in this column
cellEditor
- 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 Details
setModelIndex
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 theTableColumn
is moved around in the view the model index remains constant.
Parameters:
modelIndex
- the new modelIndexgetModelIndex
public int getModelIndex()
Returns the model index for this column.
Returns:
themodelIndex
propertysetIdentifier
Sets the
TableColumn
's identifier toanIdentifier
.
Note: identifiers are not used by theJTable
, they are purely a convenience for the external tagging and location of columns.
Parameters:
identifier
- an identifier for this column
See Also:
getIdentifier()getIdentifier
public Object getIdentifier()
Returns theidentifier
object for this column. Note identifiers are not used byJTable
, they are purely a convenience for external use. If theidentifier
isnull
,getIdentifier()
returnsgetHeaderValue
as a default.
Returns:
theidentifier
property
See Also:
setIdentifier(java.lang.Object)setCellRenderer
Sets the
TableCellRenderer
used byJTable
to draw individual values for this column.
Parameters:
cellRenderer
- the new cellRenderer
See Also:
getCellRenderer()getCellRenderer
Returns the
TableCellRenderer
used by theJTable
to draw values for this column. ThecellRenderer
of the column not only controls the visual look for the column, but is also used to interpret the value object supplied by theTableModel
. When thecellRenderer
isnull
, theJTable
uses a default renderer based on the class of the cells in that column. The default value for acellRenderer
isnull
.
Returns:
thecellRenderer
property
See Also:
setCellRenderer(javax.swing.table.TableCellRenderer), JTable.setDefaultRenderer(java.lang.Class<?>, javax.swing.table.TableCellRenderer)setCellEditor
Sets the editor to used by when a cell in this column is edited.
Parameters:
cellEditor
- the new cellEditor
See Also:
getCellEditor()getCellEditor
Returns the
TableCellEditor
used by theJTable
to edit values for this column. When thecellEditor
isnull
, theJTable
uses a default editor based on the class of the cells in that column. The default value for acellEditor
isnull
.
Returns:
thecellEditor
property
See Also:
setCellEditor(javax.swing.table.TableCellEditor), JTable.setDefaultEditor(java.lang.Class<?>, javax.swing.table.TableCellEditor)setWidth
This method should not be used to set the widths of columns in the
JTable
, usesetPreferredWidth
instead. Like a layout manager in the AWT, theJTable
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 towidth
. Ifwidth
exceeds the minimum or maximum width, it is adjusted to the appropriate limiting value.
Parameters:
width
- the new width
See Also:
getWidth(), setMinWidth(int), setMaxWidth(int), setPreferredWidth(int), JTable.doLayout()getWidth
public int getWidth()
Returns the width of theTableColumn
. The default width is 75.
Returns:
thewidth
property
See Also:
setWidth(int)setPreferredWidth
@BeanProperty(description="The preferred width of the column.")public void setPreferredWidth(int preferredWidth)
Sets this column's preferred width topreferredWidth
. IfpreferredWidth
exceeds the minimum or maximum width, it is adjusted to the appropriate limiting value.
For details on how the widths of columns in theJTable
(andJTableHeader
) are calculated from thepreferredWidth
, see thedoLayout
method inJTable
.
Parameters:
preferredWidth
- the new preferred width
See Also:
getPreferredWidth(), JTable.doLayout()getPreferredWidth
public int getPreferredWidth()
Returns the preferred width of theTableColumn
. The default preferred width is 75.
Returns:
thepreferredWidth
property
See Also:
setPreferredWidth(int)setMinWidth
@BeanProperty(description="The minimum width of the column.")public void setMinWidth(int minWidth)
Sets theTableColumn
's minimum width tominWidth
, adjusting the new minimum width if necessary to ensure that 0 <=minWidth
<=maxWidth
. For example, if theminWidth
argument is negative, this method sets theminWidth
property to 0.
If the value of thewidth
orpreferredWidth
property is less than the new minimum width, this method sets that property to the new minimum width.
Parameters:
minWidth
- the new minimum width
See Also:
getMinWidth(), setPreferredWidth(int), setMaxWidth(int)getMinWidth
public int getMinWidth()
Returns the minimum width for theTableColumn
. TheTableColumn
's width can't be made less than this either by the user or programmatically.
Returns:
theminWidth
property
See Also:
setMinWidth(int), TableColumn(int, int, TableCellRenderer, TableCellEditor)setMaxWidth
@BeanProperty(description="The maximum width of the column.")public void setMaxWidth(int maxWidth)
Sets theTableColumn
's maximum width tomaxWidth
or, ifmaxWidth
is less than the minimum width, to the minimum width.
If the value of thewidth
orpreferredWidth
property is more than the new maximum width, this method sets that property to the new maximum width.
Parameters:
maxWidth
- the new maximum width
See Also:
getMaxWidth(), setPreferredWidth(int), setMinWidth(int)getMaxWidth
public int getMaxWidth()
Returns the maximum width for theTableColumn
. TheTableColumn
's width can't be made larger than this either by the user or programmatically. The default maxWidth is Integer.MAX_VALUE.
Returns:
themaxWidth
property
See Also:
setMaxWidth(int)setResizable
@BeanProperty(description="Whether or not this column can be resized.")public void setResizable(boolean isResizable)
Sets whether this column can be resized.
Parameters:
isResizable
- if true, resizing is allowed; otherwise false
See Also:
getResizable()getResizable
public boolean getResizable()
Returns true if the user is allowed to resize theTableColumn
's width, false otherwise. You can change the width programmatically regardless of this setting. The default is true.
Returns:
theisResizable
property
See Also:
setResizable(boolean)sizeWidthToFit
public void sizeWidthToFit()
Resizes theTableColumn
to fit the width of its header cell. This method does nothing if the header renderer isnull
(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(int)disableResizedPosting
@Deprecated public void disableResizedPosting()
This field was not used in previous releases and there are currently no plans to support it in the future.enableResizedPosting
@Deprecated public void enableResizedPosting()
This field was not used in previous releases and there are currently no plans to support it in the future.addPropertyChangeListener
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
- the listener to be added
See Also:
removePropertyChangeListener(PropertyChangeListener)removePropertyChangeListener
Removes a
PropertyChangeListener
from the listener list. ThePropertyChangeListener
to be removed was registered for all properties.
Parameters:
listener
- the listener to be removedgetPropertyChangeListeners
Returns an array of all the
PropertyChangeListener
s added to this TableColumn with addPropertyChangeListener().
Returns:
all of thePropertyChangeListener
s added or an empty array if no listeners have been added
Since:
1.4