oreilly java swing : JTable 之 Table Data
2011-10-20 13:32
204 查看
15.3 Table DataWe've seen theTableColumnModel, 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 InterfaceAll 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 haveaccess 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. |
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.
Property | Data type | get | is | set | Default value |
---|---|---|---|---|---|
columnCount | int | · | |||
rowCount | int | · |
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.
相关文章推荐
- oreilly java swing : JTable 之Selection Renderer Editor
- oreilly java swing : JTable 之TableColumn
- oreilly java swing : JTable 之JTable
- JavaSwing_4.8: JTable(表格)
- Java从入门到精通 - Swing表格自定义JTable
- (整理)Java--Swing的一些记录(JTable及其他)
- 【Java】可视化Swing中JTable控件绑定SQL数据源的两种方法
- Java Swing中JTable的使用
- Java Swing JTable 表格【2:创建空表格】
- Java Swing (JTable详解1)
- Java Swing JTable 表格【16:复选框JCheckBox作为表格元素】
- Java.swing.JTable
- java swing的jtree,jtable,jlist都要设置一个model的类的原因
- oreilly java swing : JTable 之 JTableHeader
- JavaSwing 表格JTable内容居中 自定义TableModel
- JAVA-SWING:生成透明JTable的改进1
- Java Swing JTable 表格【17:使用编辑器验证文本】
- java swing jtable外观属性一
- Java Swing JTable 表格【8:表格使用选择器SelectionModel】
- Java Swing 在 JTable 中给 JCheckbox 绑定事件的方法