我如何通过一个动态数组或ArrayList fwith值从数据库中填充JTable ?

时间:2022-06-14 05:59:19

I have 6 columns and a maximum of about 20 rows in the database. I like to make a general program in Java that populates the JTable with these values. I am very much conversant with using dynamic arrays, but not familiar with JTable attributes and model in Swing.

在数据库中,我有6个列和最多20行。我喜欢用Java编写一个通用程序,用这些值填充JTable。我非常熟悉使用动态数组,但不熟悉Swing中的JTable属性和模型。

Can anyone guide me here on tho requirement? Thanks

有谁能指点我一下这方面的要求吗?谢谢

3 个解决方案

#1


2  

You have to create your own table model. See the API documentation here: http://docs.oracle.com/javase/7/docs/api/javax/swing/table/TableModel.html

您必须创建自己的表模型。参见这里的API文档:http://docs.oracle.com/javase/7/docs/api/javax/swing/table/TableModel.html。

Basically, getColumnCount() would return your number of columns, getRowCount() the number of rows in your database, getColumnName(int columnIndex) some name for every column (the column name from the database or an arbitrary name, maybe from a constant string array). getColumnClass(int columnIndex) can, in the simple case, return String.class for every column. Then you have to convert every value to string.

基本上,getColumnCount()会返回你的列数,getRowCount()在你的数据库中的行数,getColumnName(int columnIndex)每个列的名称(来自数据库的列名或任意名称,可能来自一个常量字符串数组)。在简单的情况下,getColumnClass(int columnIndex)可以返回字符串。每一列的类。然后你必须将每个值转换为字符串。

getValueAt(int rowIndex, int columnIndex) has to return the value from the database for the given row and column. You should probably pre-load all these values in a 2D array or something like this (but that would be the answer to another question ;) ).

getValueAt(int rowIndex, int columnIndex)必须从数据库返回给定行和列的值。您应该在一个2D数组中预加载所有这些值,或者类似的东西(但这将是另一个问题的答案)。

You can ignore the other methods for now, they are for editable tables.

现在可以忽略其他方法,它们用于可编辑的表。

Your code could look something like this:

你的代码可以是这样的:

String[][] tableData = readTableDataFromDatabase();
String columnNames = initColumnNames(); //From DB or constants
TableModel model = new DbTableModel(tableData, columnNames);

class DbTableModel extends AbstractTableModel {
    private String[][] tableData;
    private String[] columnNames;

    public DbTableModel(String[][] tableData, columnNames) {
        this.tableData = tableData;
        this.columnNames = columnNames;
    }
    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        return tableData[rowIndex][columnIndex];
    }
    @Override
    public int getRowCount() {
        return tableData.length;
    }
    @Override
    public int getColumnCount() {
        if(tableData.length == 0)
            return 0;
        return tableData[0].length;
    }
    @Override
    public String getColumnName(int column) {
        return columnNames[column]
    }
}

This example assumes that you read the data from the database as a 2D array. If you use an O/R-Mapper (like the Java Persistence API - JPA), which I highly recommend, you would probably load a list of Entities. Each entity would contain the data for a table row. Anyway, the table model would not change much. Instead of accessing array values, you would get an entity object from the list and call "get"-Methods on the Entity.

本例假设您将数据库中的数据作为2D数组读取。如果您使用的是O/R-Mapper(如Java Persistence API - JPA),我强烈推荐,您可能会加载一个实体列表。每个实体将包含表行的数据。无论如何,表格模型不会有太大变化。与访问数组值不同,您将从列表中获取一个实体对象,并调用实体上的“get”方法。

There's more information in the Java tutorial: http://docs.oracle.com/javase/tutorial/uiswing/components/table.html#data

在Java教程中有更多的信息:http://docs.oracle.com/javase/tutorial/uiswing/components/table.html#数据。

#2


0  

Have a look at the tutorial here: http://docs.oracle.com/javase/tutorial/uiswing/components/table.html#data

看看这里的教程:http://docs.oracle.com/javase/tutorial/uiswing/components/table.html#数据?

Here's a sample of a List-based read-only TableModel I wrote some time ago:

下面是我在一段时间之前写的一个基于列表的只读表符的示例:

http://puces-samples.svn.sourceforge.net/viewvc/puces-samples/tags/sessionstate-1.0/sessionstate-suite/sessionstate-sample/src/blogspot/puce/sessionstate/sample/ParticipantTableModel.java?revision=13&view=markup

http://puces - samples.svn.sourceforge.net/viewvc/puces samples/tags/sessionstate - 1.0 / - sessionstate suite/sessionstate sample/src/blogspot/puce/sessionstate/sample/participanttablemodel.java?revision=13&view=markup

#3


0  

Nice but i hope readTableDataFromDatabase(); return resultset object

Nice,但我希望readTableDataFromDatabase();返回结果集对象

#1


2  

You have to create your own table model. See the API documentation here: http://docs.oracle.com/javase/7/docs/api/javax/swing/table/TableModel.html

您必须创建自己的表模型。参见这里的API文档:http://docs.oracle.com/javase/7/docs/api/javax/swing/table/TableModel.html。

Basically, getColumnCount() would return your number of columns, getRowCount() the number of rows in your database, getColumnName(int columnIndex) some name for every column (the column name from the database or an arbitrary name, maybe from a constant string array). getColumnClass(int columnIndex) can, in the simple case, return String.class for every column. Then you have to convert every value to string.

基本上,getColumnCount()会返回你的列数,getRowCount()在你的数据库中的行数,getColumnName(int columnIndex)每个列的名称(来自数据库的列名或任意名称,可能来自一个常量字符串数组)。在简单的情况下,getColumnClass(int columnIndex)可以返回字符串。每一列的类。然后你必须将每个值转换为字符串。

getValueAt(int rowIndex, int columnIndex) has to return the value from the database for the given row and column. You should probably pre-load all these values in a 2D array or something like this (but that would be the answer to another question ;) ).

getValueAt(int rowIndex, int columnIndex)必须从数据库返回给定行和列的值。您应该在一个2D数组中预加载所有这些值,或者类似的东西(但这将是另一个问题的答案)。

You can ignore the other methods for now, they are for editable tables.

现在可以忽略其他方法,它们用于可编辑的表。

Your code could look something like this:

你的代码可以是这样的:

String[][] tableData = readTableDataFromDatabase();
String columnNames = initColumnNames(); //From DB or constants
TableModel model = new DbTableModel(tableData, columnNames);

class DbTableModel extends AbstractTableModel {
    private String[][] tableData;
    private String[] columnNames;

    public DbTableModel(String[][] tableData, columnNames) {
        this.tableData = tableData;
        this.columnNames = columnNames;
    }
    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        return tableData[rowIndex][columnIndex];
    }
    @Override
    public int getRowCount() {
        return tableData.length;
    }
    @Override
    public int getColumnCount() {
        if(tableData.length == 0)
            return 0;
        return tableData[0].length;
    }
    @Override
    public String getColumnName(int column) {
        return columnNames[column]
    }
}

This example assumes that you read the data from the database as a 2D array. If you use an O/R-Mapper (like the Java Persistence API - JPA), which I highly recommend, you would probably load a list of Entities. Each entity would contain the data for a table row. Anyway, the table model would not change much. Instead of accessing array values, you would get an entity object from the list and call "get"-Methods on the Entity.

本例假设您将数据库中的数据作为2D数组读取。如果您使用的是O/R-Mapper(如Java Persistence API - JPA),我强烈推荐,您可能会加载一个实体列表。每个实体将包含表行的数据。无论如何,表格模型不会有太大变化。与访问数组值不同,您将从列表中获取一个实体对象,并调用实体上的“get”方法。

There's more information in the Java tutorial: http://docs.oracle.com/javase/tutorial/uiswing/components/table.html#data

在Java教程中有更多的信息:http://docs.oracle.com/javase/tutorial/uiswing/components/table.html#数据。

#2


0  

Have a look at the tutorial here: http://docs.oracle.com/javase/tutorial/uiswing/components/table.html#data

看看这里的教程:http://docs.oracle.com/javase/tutorial/uiswing/components/table.html#数据?

Here's a sample of a List-based read-only TableModel I wrote some time ago:

下面是我在一段时间之前写的一个基于列表的只读表符的示例:

http://puces-samples.svn.sourceforge.net/viewvc/puces-samples/tags/sessionstate-1.0/sessionstate-suite/sessionstate-sample/src/blogspot/puce/sessionstate/sample/ParticipantTableModel.java?revision=13&view=markup

http://puces - samples.svn.sourceforge.net/viewvc/puces samples/tags/sessionstate - 1.0 / - sessionstate suite/sessionstate sample/src/blogspot/puce/sessionstate/sample/participanttablemodel.java?revision=13&view=markup

#3


0  

Nice but i hope readTableDataFromDatabase(); return resultset object

Nice,但我希望readTableDataFromDatabase();返回结果集对象