赞
踩
The
的
-
- DefaultTableModel
class is a subclass of the
类是的子类
-
- AbstractTableModel
. As the name suggests it is the table model that is used by a
。 顾名思义,它是由表模型使用的。
when no table model is specifically defined by the programmer. The DefaultTableModel stores the data for the JTable in a
当程序员没有专门定义表模型时。 DefaultTableModel将JTable的数据存储在一个
-
- Vector
of
的
-
- Vectors
.
。
Although the
虽然
-
- Vector
is a legacy Java collection it is still supported and there is no issue with using it unless the additional overhead caused by using a synchronized collection is a problem for your Java application.
是一个旧的Java集合,它仍然受支持,除非使用同步集合引起的额外开销是Java应用程序的问题,否则使用它没有问题。
The advantage of using the
使用的优势
-
- DefaultTableModel
over a custom
在风俗上
-
- AbstractTableModel
is you don't have to code the methods like add, insert or delete rows and columns. They already exist to change the data held in the
是您不必编写添加,插入或删除行和列之类的方法的代码。 它们已经存在,可以更改保存在
-
- Vector
of
的
-
- Vectors.
This makes it a quick and easy table model to implement.
这使得它可以快速,轻松地实现表模型。
-
- import javax.swing.table.DefaultTableModel;
The
的
-
- DefaultTableModel
class has six
全班有六个
. Each can be used to populate of the
。 每个都可以用来填充
-
- DefaultTableModel
in different ways.
以不同的方式。
The first constructor takes no arguments and creates a
第一个构造函数不带参数,并创建一个
-
- DefaultTableModel
which has no data, zero columns and zero rows:
没有数据,零列和零行:
-
- DefaultTableModel defTableModel = DefaultTableModel();
The next constructor can be used to specify the number of rows and columns of a
下一个构造函数可用于指定一个对象的行数和列数
-
- DefaultTableModel
with no data:
没有数据:
-
- DefaultTableModel defTableModel = DefaultTableModel(10, 10);
There are two constructors that can be used to create a
有两个构造函数可用于创建一个
-
- DefaultTableModel
with column names and a specified number of rows (all containing null values). One uses an
具有列名和指定的行数(均包含空值)。 一个使用
-
- Object
array to hold the column names, the other a
存放列名的数组,另一个
-
- Vector
:
:
or
要么
-
- DefaultTableModel defTableModel = DefaultTableModel(columnNames, 10);
Finally there are two constructors used to populate the
最后,有两个构造函数用于填充
-
- DefaultTableModel
with row data along with column names. One used
行数据和列名。 一用
-
- Object
arrays, the other
数组,另一个
-
- Vectors
:
:
or
要么
To add a row to the
向行添加行
-
- DefaultTableModel
use the
使用
-
- addRow
method along with the row data to add:
方法以及要添加的行数据:
To insert a row use the
要插入一行,请使用
-
- insertRow
method, specifying the row index to insert and the row data:
方法,指定要插入的行索引和行数据:
To delete a row use the
要删除行,请使用
-
- removeRow
method, specifying the row index to delete:
方法,指定要删除的行索引:
-
- defTableModel.removeRow(0);
To get a value in a table cell use the
要在表格单元格中获取值,请使用
-
- getValueAt
method. For example, if the data at row 2, column 2 contains an int:
方法。 例如,如果第2行的数据第2列包含一个int:
-
- int value = tabModel.getValueAt(2, 2);
To set a value in a table cell
在表格单元格中设置值
-
- setValueAt
method with the value to set along with the row and column index:
具有要设置的值以及行和列索引的方法:
-
- defTableModel.setValueAt(8888, 3, 2);
If a
如果一个
-
- JTable
is created using the constructor that is passed a two-dimensional array containing the row data and an array containing the column names:
使用构造函数创建,该构造函数将传递包含行数据的二维数组和包含列名的数组:
then the following cast will not work:
那么以下强制转换将无效:
A runtime
运行时
-
- ClassCastException
will be thrown because in this instance the
将被抛出,因为在这种情况下
-
- DefaultTableModel
is declared as an
被声明为
in the
在里面
-
- JTable
object and cannot be cast. It can only be cast to the
对象,无法投射。 它只能投射到
-
- TableModel
interface. A way around this is to create your own
接口。 解决这个问题的一种方法是创建自己的
-
- DefaultTableModel
and set it to be the model of the
并将其设置为
-
- JTable
:
:
Then the
然后
-
- DefaultTableModel
-
- defTableModel
can be used to manipulate the data in the
可用于操作
-
- JTable
.
。
To see the
看到
-
- DefaultTableModel
in action have a look at the
在行动中看看
.
。
翻译自: https://www.thoughtco.com/defaulttablemodel-overview-2033890
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。