使用对象数组初始化表
我在使用 BD 或数组从 FOR 初始化 JTable 时遇到问题。
我的问题是示例 tableFilterDemo.java,我需要它的功能, 但是当我想要加载 BD 或数组列表的数据时,我遇到了问题。
我需要使用 FOR 加载对象数组来获取文件的所有行或表的行
private Object[][] data = {
{ "Mary", "Campione", "Snowboarding"},
{ "John", "guifru", "skyiin"},};
I am having a problem initializing a JTable from a FOR with BD or an array.
My problem is the example tableFilterDemo.java, I need the funcionality of this,
but when I want load data of my BD or an arraylist I have the problem.
I need load the array of objects with a FOR getting all the lines of file or rows of table
private Object[][] data = {
{ "Mary", "Campione", "Snowboarding"},
{ "John", "guifru", "skyiin"},};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您始终可以使用 DefaultTableModel,它使用向量的向量来保存 TableModel 中的数据。因此,对于表中的每一行,您创建一个向量并将每一列添加到向量中。然后将行向量添加到第二个向量。这样您就不需要提前对数组的大小进行硬编码。 数据库中的表展示了如何使用此方法。
或者,如果您想显示存储在 ArrayList 中的自定义对象,则始终可以使用自定义 TableModel。 行表模型为将对象存储在一个数组列表。您还需要查看 BeanTableModel 的完整实现以及如何进行自定义实现的进一步示例。
You can always use the DefaultTableModel which uses a Vector of Vectors to hold the data in the TableModel. So for each row in the table you create a Vector and add each column to the Vecter. Then you add the row Vector to a second Vector. This way you don't need to hardcode the size of the Arrays in advance. Table From Database shows how you can use this approach.
Or you can always use a custom TableModel if you want to display custom Ojects that are stored in an ArrayList. The Row Table Model provides some general support for storing objects in an ArrayList. You would also want to look at the
BeanTableModel
for a full implementation and a further example of how to do a custom implementation.您有两种方法:
JTable(Object[][] rowData, Object[] columnNames)
)将对象直接设置到JTable
这非常简单而且很好,但只是管理AbstractTableModel
的类,在其中您重写正确的方法来映射右侧二维集合行为。You have two ways:
JTable
by invoking the right constructor (which isJTable(Object[][] rowData, Object[] columnNames)
) and this is quite easy and good but just manages static tablesAbstractTableModel
in which you override the right methods to map the bidimensional collection to the right behaviors.我不确定我是否理解了你的问题。
无论如何,如果您的表有 3 列,并且您已经定义了表模型
这是该方法的声明:
setValueAt(Object aValue, int row, int column)
如果你想将 arrayList 转换为数组,你可以:
再见卢卡
I'm not sure that I have understood your problem.
Anyway, if your table have 3 columns and you have defined the table model as defined here, you have to itrate your array and put the values in the table in this way:
This is the declaration of the method:
setValueAt(Object aValue, int row, int column)
If you want to convert an arrayList to an array you can:
Bye Luca