The TableColumn class is the starting point for building your columns. It supplies access to all the basic components of an individual column. This class should not be confused with theTableColumnModel interface. That model, discussed in the next section, dictates the form of a collection of table columns, which then makes up a full table.
The TableColumn class has the properties listed in Table 15-5.
Property
Data type
get
is
set
Default value
cellEditor |
TableCellEditor |
· |
· |
null |
|
cellRendererb |
TableCellRenderer |
· |
· |
null |
|
headerRendererb |
TableCellRenderer |
· |
· |
null |
|
headerValueb |
Object |
· |
· |
null |
|
identifier |
Object |
· |
· |
null |
|
maxWidth |
int |
· |
· |
Integer.MAX_VALUE |
|
minWidth |
int |
· |
· |
15 |
|
modelIndex |
int |
· |
· |
0 |
|
propertyChangeListeners1.4 |
PropertyChangeListener[] |
· |
Empty array |
||
resizable |
boolean |
· |
· |
true |
|
widthb |
int |
· |
· |
75 |
|
bbound, 1.4since 1.4 |
The cellEditor,cellRenderer, and headerRenderer properties determine which components are used to draw (and possibly edit) cell values. The default value ofnull for these properties indicates that a default renderer or editor should be built and used. TheheaderValue property is accessible to theheaderRenderer for drawing an appropriate header. The identifier property is used to identify a column uniquely. If anidentifier is not specified, thegetIdentifier( ) method returns the currentheaderValue. The minWidth and maxWidth properties determine the minimum and maximum width in pixels for the column. By setting these properties to the same value, you can create a fixed-width column. The current width of the column is stored in thewidth property. The modelIndex determines the index value used when rendering or editing the column to get the appropriate data values. It is normally set in the constructor and does not need to be modified after that.Relocating the column onscreen has no effect on the model index. Theresizable property affects only the user's ability to manually resize columns?you can programmatically resize a column at any time.
当一个表格的TableColumnModel创建后,会添加相应的TableColumn,TableColumn第一次会按照顺序添加到TableColumnModel的Vector中,会创建modelIndex,就算以你对表格操作做过任何处理(自己不在程序中修改modelIndex) ,那么modelIndex是不会改变的,比如你拖动列,那么只是把此列在Vector中的顺序改变,但是modelIndex还是不会改变.
Four of the properties have descriptive constants that are used with property change events. These constants are listed inTable 15-6.
Constant
Type
Description
CELL_RENDERER_PROPERTY |
String |
The property name of the cellRenderer property |
COLUMN_WIDTH_PROPERTY |
String |
The property name of the columnWidth property |
HEADER_RENDERER_PROPERTY |
String |
The property name of the headerRenderer property |
HEADER_VALUE_PROPERTY |
String |
The property name of the headerValue property |
The only event generated by theTableColumn class is a property change event that is generated when any of the column's bound properties (cellRenderer,headerRenderer,headerValue, and width) are changed.
Add or remove a property change listener interested in receiving events from this column.
The following constructors exist for building TableColumn objects:
Create an empty column with the default property values.
Create an empty column with the specified modelIndex.
Create an empty column with the specified modelIndex andwidth in pixels. TheminWidth and maxWidth properties keep their default values.
Create an empty column with the specified modelIndex,width in pixels,cellRenderer, and cellEditor. The renderer or editor arguments can benull, in which case the appropriate default is used.
While the get/set methods for the properties constitute a majority of theTableColumn class, the following method does provide a bit of functionality:
Force the width of the column to match that of its header, even if it means modifying theminWidth ormaxWidth properties.
A single column is not a very interesting table?an interesting list, maybe, but not a table. To handle real tables (even ones with only one column), we need a model for storing several columns as a collection. The TableColumnModel interface provides that functionality in the Swing package. As you may have noticed fromFigure 15-2, theJTable class has a column model in addition to a table model. While the table model provides the specific values for the cells in a column,the column model provides information such as the column margins and whether or not column selections are allowed.
The TableColumnModel interface manages column selections and column spacing. For managing selections, you have access to the usual selection properties, such as the number of selected columns and the selection model in place. For dealing with column spacing, you can control the column margins and view the total column width.
TableColumnModel has the properties listed in Table 15-7. The columnCount property returns the number of columns supported by this model. While this might seem like redundant information, given that the table model (discussed later in this chapter) knows how many columns it supports, the next chapter examines some column models that do not use all of the columns available in the table model. ThecolumnMargin property dictates how much space should be left between columns. That spacing is included when calculating the value of thetotalColumnWidth. You can turn column selection on or off with thecolumnSelectionAllowed property. If column selections are allowed, you can then use theselectionModel ,selectedColumns, and selectedColumnCount properties to work with the selections. As with other selections, you can use theselectionModel to programmatically affect the selected columns if needed.
Property
Data type
get
is
set
Default value
columni |
TableColumn |
· |
|||
columns |
Enumeration |
· |
|||
columnCount |
int |
· |
|||
columnMargin |
int |
· |
· |
||
columnSelectionAllowed |
boolean |
· |
· |
||
selectedColumnCount |
int |
· |
|||
selectedColumns |
int[] |
· |
|||
selectionModel |
ListSelectionModel |
· |
· |
||
totalColumnWidth |
int |
· |
|||
iindexed |
The column and columns properties let you access the table's columns themselves.The index used as an argument togetColumn( ) refers to the column's index in the column model, which doesn't necessarily match the index of the column in the table model, or the order in which columns appear on the screen.
Any class implementing theTableColumnModel interface has to support theColumnModelEvent,which is generated when a column's view size, position, or extent size changes. The interface defines the standardaddColumnModelListener( ) andremoveColumnModelListener( ) methods, but the implementing class is responsible for the code that fires the events when columns are added, removed, or moved.
Add or remove a listener interested in changes to this column model.
The TableColumnModel interface defines several methods for working with the columns in the model:
Append column to the current column model.
Return the column model (screen) index of a column, either with a header matchingidentifier or at the specifiedxPixel location on the screen.
Move the column at index to newIndex. Other columns should be shifted as needed to accommodate the moved column. This visually relocates the column on the screen only. The table model does not change.
Delete column from the column model. Columns following the removed column are shifted one index to fill the gap.
The DefaultTableColumnModel class implements theTableColumnModel interface and serves as the column model if you do not specify a model in theJTable constructor. It also works as a good starting point for creating your own column models. You inherit everything you need; just override the methods you want to change.
The DefaultTableColumnModel class inherits all of its properties from theTableColumnModel interface and supplies the default values shown inTable 15-8.
Property
Data type
get
is
set
Default value
columni, o |
TableColumn |
· |
|||
columnso |
Enumeration |
· |
|||
columnCounto |
int |
· |
0 |
||
columnMargino |
int |
· |
· |
1 |
|
columnSelectionAllowedo |
boolean |
· |
· |
false |
|
selectedColumnCounto |
int |
· |
0 |
||
selectedColumnso |
int[] |
· |
null |
||
selectionModelo |
ListSelectionModel |
· |
· |
DefaultListSelectionModel( ) |
|
totalColumnWidtho |
int |
· |
0 |
||
iindexed, ooverridden |
The DefaultTableColumnModel supports theColumnModelEvent events that are dictated by theTableColumnModel interface, but it includes several convenience methods beyond simply attaching listeners:
These helper methods are used to fire events when columns are added, resized, relocated, removed, or selected. They also allow you to fire column-based events to create your own column model by extending this class.
Add or remove a listener for TableColumnModelEvents fired by this model.
The DefaultTableColumnModel listens to some of these events to keep the visual state of the table in sync. TheCOLUMN_WIDTH_PROPERTY change events (from one of the addedTableColumn objects) cause the width cache for the table to be recalculated. ThevalueChanged( ) method listens for new column selections and fires a column selection changed event.
Set up a new DefaultTableColumnModel with the default values for properties listed inTable 15-8.
These methods provide straightforward implementation of the abstract methods required by theTableColumnModel interface.
Many of the events fired by theDefaultTableColumnModel class use this event class to encode the columns that were affected. Notice that these events describe something happening to a contiguous group of columns, unlike the selection events. There is no direct support for things like removing a discontiguous selection of columns. You must generate a different event for each contiguous range of columns that needs to be removed.
Use these methods to find the affected columns. If only a single column is affected, these methods return the same value.
If you want to listen to any of the column model events, you must implement theTableColumnModelListener interface and register as a listener for these events. Not surprisingly, the event-firing methods from theDefaultTableColumnModel class reflect the types of events this interface defines:
Use these methods to react to changes in the column model. While you cannot add aListSelectionListener directly to the column model if you care only about column selections, you can retrieve the selection model (usinggetSelectionModel( )) and attach a listener to that object.