- All Implemented Interfaces:
Serializable
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 XMLEncoder.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringObsolete as of Java 2 platform v1.3.protected TableCellEditorThe editor used to edit the data cells of the column.protected TableCellRendererThe renderer used to draw the data cells of the column.static final StringObsolete as of Java 2 platform v1.3.static final StringObsolete as of Java 2 platform v1.3.static final StringObsolete as of Java 2 platform v1.3.protected TableCellRendererThe renderer used to draw the header of the column.protected ObjectThe header value of the column.protected ObjectThis object is not used internally by the drawing machinery of theJTable; identifiers may be set in theTableColumnas an optional way to tag and locate table columns.protected booleanIf true, the user is allowed to resize the column; the default is true.protected intThe maximum width of the column.protected intThe minimum width of the column.protected intThe index of the column in the model which is to be displayed by thisTableColumn.protected intDeprecated.as of Java 2 platform v1.3protected intThe width of the column. -
Constructor Summary
ConstructorsConstructorDescriptionCover method, using a default model index of 0, default width of 75, anullrenderer and anulleditor.TableColumn(int modelIndex) Cover method, using a default width of 75, anullrenderer and anulleditor.TableColumn(int modelIndex, int width) Cover method, using anullrenderer and anulleditor.TableColumn(int modelIndex, int width, TableCellRenderer cellRenderer, TableCellEditor cellEditor) Creates and initializes an instance ofTableColumnwith the specified model index, width, cell renderer, and cell editor; allTableColumnconstructors delegate to this one. -
Method Summary
Modifier and TypeMethodDescriptionvoidAdds aPropertyChangeListenerto the listener list.protected TableCellRendererAs of Java 2 platform v1.3, this method is not called by theTableColumnconstructor.voidDeprecated.as of Java 2 platform v1.3voidDeprecated.as of Java 2 platform v1.3Returns theTableCellEditorused by theJTableto edit values for this column.Returns theTableCellRendererused by theJTableto draw values for this column.Returns theTableCellRendererused to draw the header of theTableColumn.Returns theObjectused as the value for the header renderer.Returns theidentifierobject for this column.intReturns the maximum width for theTableColumn.intReturns the minimum width for theTableColumn.intReturns the model index for this column.intReturns the preferred width of theTableColumn.Returns an array of all thePropertyChangeListeners added to this TableColumn with addPropertyChangeListener().booleanReturns true if the user is allowed to resize theTableColumn's width, false otherwise.intgetWidth()Returns the width of theTableColumn.voidRemoves aPropertyChangeListenerfrom the listener list.voidsetCellEditor(TableCellEditor cellEditor) Sets the editor to used by when a cell in this column is edited.voidsetCellRenderer(TableCellRenderer cellRenderer) Sets theTableCellRendererused byJTableto draw individual values for this column.voidsetHeaderRenderer(TableCellRenderer headerRenderer) Sets theTableCellRendererused to draw theTableColumn's header toheaderRenderer.voidsetHeaderValue(Object headerValue) Sets theObjectwhose string representation will be used as the value for theheaderRenderer.voidsetIdentifier(Object identifier) Sets theTableColumn's identifier toanIdentifier.voidsetMaxWidth(int maxWidth) Sets theTableColumn's maximum width tomaxWidthor, ifmaxWidthis less than the minimum width, to the minimum width.voidsetMinWidth(int minWidth) Sets theTableColumn's minimum width tominWidth, adjusting the new minimum width if necessary to ensure that 0 <=minWidth<=maxWidth.voidsetModelIndex(int modelIndex) Sets the model index for this column.voidsetPreferredWidth(int preferredWidth) Sets this column's preferred width topreferredWidth.voidsetResizable(boolean isResizable) Sets whether this column can be resized.voidsetWidth(int width) This method should not be used to set the widths of columns in theJTable, usesetPreferredWidthinstead.voidResizes theTableColumnto fit the width of its header cell.
-
Field Details
-
COLUMN_WIDTH_PROPERTY
Obsolete as of Java 2 platform v1.3. Please use string literals to identify properties.- See Also:
-
HEADER_VALUE_PROPERTY
Obsolete as of Java 2 platform v1.3. Please use string literals to identify properties.- See Also:
-
HEADER_RENDERER_PROPERTY
Obsolete as of Java 2 platform v1.3. Please use string literals to identify properties.- See Also:
-
CELL_RENDERER_PROPERTY
Obsolete as of Java 2 platform v1.3. Please use string literals to identify properties.- See Also:
-
modelIndex
protected int modelIndexThe index of the column in the model which is to be displayed by thisTableColumn. As columns are moved around in the viewmodelIndexremains constant. -
identifier
This object is not used internally by the drawing machinery of theJTable; identifiers may be set in theTableColumnas 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 theequalsmethod which is used in thegetColumnIndex()method in theDefaultTableColumnModel. -
width
protected int widthThe width of the column. -
minWidth
protected int minWidthThe minimum width of the column. -
maxWidth
protected int maxWidthThe maximum width of the column. -
headerRenderer
The renderer used to draw the header of the column. -
headerValue
The header value 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 isResizableIf true, the user is allowed to resize the column; the default is true. -
resizedPostingDisableCount
Deprecated.as of Java 2 platform v1.3This 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, anullrenderer and anulleditor. This method is intended for serialization. -
TableColumn
public TableColumn(int modelIndex) Cover method, using a default width of 75, anullrenderer and anulleditor.- 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
public TableColumn(int modelIndex, int width) Cover method, using anullrenderer and anulleditor.- 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 viewwidth- this column's preferred width and initial width- See Also:
-
TableColumn
public TableColumn(int modelIndex, int width, TableCellRenderer cellRenderer, TableCellEditor cellEditor) Creates and initializes an instance ofTableColumnwith the specified model index, width, cell renderer, and cell editor; allTableColumnconstructors delegate to this one. The value ofwidthis used for both the initial and preferred width; ifwidthis 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
cellRendererorcellEditorparameter isnull, a default value provided by theJTablegetDefaultRendererorgetDefaultEditormethod, 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 thegetCellRenderermethods 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 viewwidth- this column's preferred width and initial widthcellRenderer- the object used to render values in this columncellEditor- the object used to edit values in this column- See Also:
-
-
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 thisTableColumn. As theTableColumnis moved around in the view the model index remains constant.- Parameters:
modelIndex- the new modelIndex
-
getModelIndex
public int getModelIndex()Returns the model index for this column.- Returns:
- the
modelIndexproperty
-
setIdentifier
@BeanProperty(description="A unique identifier for this column.") public void setIdentifier(Object identifier) Sets theTableColumn's identifier toanIdentifier.Note: identifiers are not used by the
JTable, they are purely a convenience for the external tagging and location of columns.- Parameters:
identifier- an identifier for this column- See Also:
-
getIdentifier
Returns theidentifierobject for this column. Note identifiers are not used byJTable, they are purely a convenience for external use. If theidentifierisnull,getIdentifier()returnsgetHeaderValueas a default.- Returns:
- the
identifierproperty - See Also:
-
setHeaderValue
@BeanProperty(description="The text to be used by the header renderer.") public void setHeaderValue(Object headerValue) Sets theObjectwhose string representation will be used as the value for theheaderRenderer. When theTableColumnis created, the defaultheaderValueisnull.- Parameters:
headerValue- the new headerValue- See Also:
-
getHeaderValue
Returns theObjectused as the value for the header renderer.- Returns:
- the
headerValueproperty - See Also:
-
setHeaderRenderer
@BeanProperty(description="The header renderer.") public void setHeaderRenderer(TableCellRenderer headerRenderer) Sets theTableCellRendererused to draw theTableColumn's header toheaderRenderer.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- the new headerRenderer- See Also:
-
getHeaderRenderer
Returns theTableCellRendererused to draw the header of theTableColumn. When theheaderRendererisnull, theJTableHeaderuses itsdefaultRenderer. The default value for aheaderRendererisnull.- Returns:
- the
headerRendererproperty - See Also:
-
setCellRenderer
@BeanProperty(description="The renderer to use for cell values.") public void setCellRenderer(TableCellRenderer cellRenderer) Sets theTableCellRendererused byJTableto draw individual values for this column.- Parameters:
cellRenderer- the new cellRenderer- See Also:
-
getCellRenderer
Returns theTableCellRendererused by theJTableto draw values for this column. ThecellRendererof the column not only controls the visual look for the column, but is also used to interpret the value object supplied by theTableModel. When thecellRendererisnull, theJTableuses a default renderer based on the class of the cells in that column. The default value for acellRendererisnull.- Returns:
- the
cellRendererproperty - See Also:
-
setCellEditor
@BeanProperty(description="The editor to use for cell values.") public void setCellEditor(TableCellEditor cellEditor) Sets the editor to used by when a cell in this column is edited.- Parameters:
cellEditor- the new cellEditor- See Also:
-
getCellEditor
Returns theTableCellEditorused by theJTableto edit values for this column. When thecellEditorisnull, theJTableuses a default editor based on the class of the cells in that column. The default value for acellEditorisnull.- Returns:
- the
cellEditorproperty - See Also:
-
setWidth
This method should not be used to set the widths of columns in theJTable, usesetPreferredWidthinstead. Like a layout manager in the AWT, theJTableadjusts 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. Ifwidthexceeds the minimum or maximum width, it is adjusted to the appropriate limiting value.- Parameters:
width- the new width- See Also:
-
getWidth
public int getWidth()Returns the width of theTableColumn. The default width is 75.- Returns:
- the
widthproperty - See Also:
-
setPreferredWidth
@BeanProperty(description="The preferred width of the column.") public void setPreferredWidth(int preferredWidth) Sets this column's preferred width topreferredWidth. IfpreferredWidthexceeds the minimum or maximum width, it is adjusted to the appropriate limiting value.For details on how the widths of columns in the
JTable(andJTableHeader) are calculated from thepreferredWidth, see thedoLayoutmethod inJTable.- Parameters:
preferredWidth- the new preferred width- See Also:
-
getPreferredWidth
public int getPreferredWidth()Returns the preferred width of theTableColumn. The default preferred width is 75.- Returns:
- the
preferredWidthproperty - See Also:
-
setMinWidth
Sets theTableColumn's minimum width tominWidth, adjusting the new minimum width if necessary to ensure that 0 <=minWidth<=maxWidth. For example, if theminWidthargument is negative, this method sets theminWidthproperty to 0.If the value of the
widthorpreferredWidthproperty 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
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:
- the
minWidthproperty - See Also:
-
setMaxWidth
Sets theTableColumn's maximum width tomaxWidthor, ifmaxWidthis less than the minimum width, to the minimum width.If the value of the
widthorpreferredWidthproperty 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
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:
- the
maxWidthproperty - See Also:
-
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
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:
- the
isResizableproperty - See Also:
-
sizeWidthToFit
public void sizeWidthToFit()Resizes theTableColumnto 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:
-
disableResizedPosting
Deprecated.as of Java 2 platform v1.3This field was not used in previous releases and there are currently no plans to support it in the future. -
enableResizedPosting
Deprecated.as of Java 2 platform v1.3This field was not used in previous releases and there are currently no plans to support it in the future. -
addPropertyChangeListener
Adds aPropertyChangeListenerto 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
Removes aPropertyChangeListenerfrom the listener list. ThePropertyChangeListenerto be removed was registered for all properties.- Parameters:
listener- the listener to be removed
-
getPropertyChangeListeners
Returns an array of all thePropertyChangeListeners added to this TableColumn with addPropertyChangeListener().- Returns:
- all of the
PropertyChangeListeners added or an empty array if no listeners have been added - Since:
- 1.4
-
createDefaultHeaderRenderer
As of Java 2 platform v1.3, this method is not called by theTableColumnconstructor. Previously this method was used by theTableColumnto create a default header renderer. As of Java 2 platform v1.3, the default header renderer isnull.JTableHeadernow provides its own shared default renderer, just as theJTabledoes for its cell renderers.- Returns:
- the default header renderer
- See Also:
-