oreilly java swing : JTable 之 Table Data

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.


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值