tof

more see,more think,more practice,more summary

oreilly java swing : JTable 之TableColumn

15.1.7 The TableColumn Class

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.

15.1.7.1 Properties

The TableColumn class has the properties listed in Table 15-5.

Table 15-5. TableColumn properties

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还是不会改变.

15.1.7.2 Constants

Four of the properties have descriptive constants that are used with property change events. These constants are listed inTable 15-6.

Table 15-6. TableColumn constants

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

15.1.7.3 Event

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.

public void addPropertyChangeListener(PropertyChangeListener l)public void removePropertyChangeListener(PropertyChangeListener l)

Add or remove a property change listener interested in receiving events from this column.

15.1.7.4 Constructors

The following constructors exist for building TableColumn objects:

public TableColumn( )

Create an empty column with the default property values.

public TableColumn(int modelIndex)

Create an empty column with the specified modelIndex.

public TableColumn(int modelIndex, int width)

Create an empty column with the specified modelIndex andwidth in pixels. TheminWidth and maxWidth properties keep their default values.

public TableColumn(int modelIndex, int width, TableCellRenderer cellRenderer, TableCellEditor cellEditor)

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.

15.1.7.5 Another useful method

While the get/set methods for the properties constitute a majority of theTableColumn class, the following method does provide a bit of functionality:

public void sizeWidthToFit( )

Force the width of the column to match that of its header, even if it means modifying theminWidth ormaxWidth properties.

15.1.8 The TableColumnModel Interface

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.

15.1.8.1 Properties

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.

Table 15-7. TableColumnModel properties

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.

15.1.8.2 Event

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.

public void addColumnModelListener(TableColumnModelListener l)public void removeColumnModelListener(TableColumnModelListener l)

Add or remove a listener interested in changes to this column model.

15.1.8.3 Column methods

The TableColumnModel interface defines several methods for working with the columns in the model:

public void addColumn(TableColumn column)

Append column to the current column model.

public int getColumnIndex(Object identifier)public int getColumnIndexAtX(int xPixel)

Return the column model (screen) index of a column, either with a header matchingidentifier or at the specifiedxPixel location on the screen.

public void moveColumn(int index, int newIndex)

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.

public void removeColumn(TableColumn column)

Delete column from the column model. Columns following the removed column are shifted one index to fill the gap.

15.1.9 The DefaultTableColumnModel Class

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.

15.1.9.1 Properties

The DefaultTableColumnModel class inherits all of its properties from theTableColumnModel interface and supplies the default values shown inTable 15-8.

Table 15-8. DefaultTableColumnModel properties

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

15.1.9.2 Events

The DefaultTableColumnModel supports theColumnModelEvent events that are dictated by theTableColumnModel interface, but it includes several convenience methods beyond simply attaching listeners:

protected void fireColumnAdded(TableColumnModelEvent e)protected void fireColumnMarginChanged( )protected void fireColumnMoved(TableColumnModelEvent e)protected void fireColumnRemoved(TableColumnModelEvent e)protected void fireColumnSelectionChanged(ListSelectionEvent e)

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.

public void addColumnModelListener(TableColumnModelListener x)public void removeColumnModelListener(TableColumnModelListener x)

Add or remove a listener for TableColumnModelEvents fired by this model.

protected void propertyChange(PropertyChangeEvent e)protected void valueChanged(ListSelectionEvent e)

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.

15.1.9.3 Constructor
public DefaultTableColumnModel( )

Set up a new DefaultTableColumnModel with the default values for properties listed inTable 15-8.

15.1.9.4 Other useful methods
public void addColumn(TableColumnColumn)public int getColumnIndex(Object identifier)public int getColumnIndexAtX(int xPixel)public void moveColumn(int index, int newindex)public void removeColumn(TableColumn column)

These methods provide straightforward implementation of the abstract methods required by theTableColumnModel interface.

15.1.10 The TableColumnModelEvent Class

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.

15.1.10.1 Event methods
public int getFromIndex( )
public int getToIndex( )

Use these methods to find the affected columns. If only a single column is affected, these methods return the same value.

15.1.11 The TableColumnModelListener Interface

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:

public void columnAdded(TableColumnModelEvent e)public void columnMarginChanged(ChangeEvent e)public void columnMoved(TableColumnModelEvent e)public void columnRemoved(TableColumnModelEvent e)public void columnSelectionChanged(ListSelectionEvent e)

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.


阅读更多
个人分类: SWING
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭