您的位置:首页 > 编程语言 > Java开发

oreilly java swing : JTable 之 Table Data

2011-10-20 13:32 204 查看

15.3 Table Data

We've seen the
TableColumnModel, which stores a lot of information about the structure of a table but doesn't contain the actual data. The data that's displayed in a
JTable is stored in a TableModel. The TableModel interface describes the minimum requirements for a model that supplies the information necessary to display and edit a table's cells and to show column headers. The
AbstractTableModel fills out most of the TableModel interface, but leaves the methods for retrieving the actual data undefined. The
DefaultTableModel extends AbstractTableModel and provides an implementation for storing data as a vector of vectors. We'll look at both the abstract and default table models in more detail later in this chapter.

15.3.1 The TableModel Interface

All of the table models start with this interface. A table model must be able to give out information on the number of rows and columns in the table and have
access to the values of the cells of the table. The TableModel interface also has methods that can be used to encode information about the columns of the table (such as a localized name or class type) separate from the column model.

15.3.1.1 Properties
The TableModel interface supports the properties shown in
Table 15-9.
The columnCount is the number of columns in the data model. This does not have to match the number of columns reported by the column model. Likewise,
rowCount is the number of rows in the data model. columnName and
columnClass are indexed properties that let you retrieve the name of the column and the class of objects in the column. The name used in the table model is distinct from anything used in the
TableColumn class. For both properties, remember that the index refers to the table model, regardless of where the column appears on the screen.

Table 15-9. TableModel properties

Property

Data type
get
is
set
Default value
columnCount
int
·
rowCount
int
·
15.3.1.2 Events
As you may have come to expect from other models in the Swing package, the
TableModel has its own event type, TableModelEvent, generated whenever the table changes. A full discussion of the
TableModelEvent class and the TableModelListener appears later in this chapter.

public void addTableModelListener(TableModelListener l)
public void removeTableModelListener(TableModelListener l)

Add or remove listeners interested in receiving table model events.

15.3.1.3 Cell methods
These methods let you obtain and change the values of individual cells:

public Object getValueAt(int rowIndex, int columnIndex)

Return the value of the cell at (rowIndex, columnIndex). Base types (int,
float, etc.) are wrapped in an appropriate Object.
public boolean isCellEditable(int rowIndex, int columnIndex)

Return true if the cell at (rowIndex, columnIndex) can be edited.

public void setValueAt(Object aValue, int rowIndex, int columnIndex)

Set the value of the cell at (rowIndex, columnIndex) to
aValue. As with the getValueAt( ) method, you may need to wrap primitive data types in an
Object (like Integer) before using them to set the value of a cell.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: