我在课堂上创建了 2 个 JTable,
对于第一个 JTable,数据来自服务器到 arraylist,然后到 JTable
对于第二个 JTable,数据来自本地访问数据库到 JTable(无数组列表)
当用户单击按钮时,两个 JTable 将一起显示。
但是在界面上加载和显示两个jtable最多需要1分钟。
有什么方法可以更快地加载它们吗?
示例代码表1
data = new ArrayList<Person>();
try
{
conn = dc.getConnection();
stmt = conn.prepareStatement(query);
rs = stmt.executeQuery();
while(rs.next())
{
String[] rowData = new String[4];
for(int i=1;i<=4;i++)
{
rowData[i-1] = rs.getString(i);
}
//database to arraylist
data.add(new Person(rowData[0],rowData[1],rowData[2],rowData[3]));
}
String[] colNames = {"LastName","FirstName","Email","Department"};
model = new DefaultTableModel(colNames,data.size())
sorter = new TableRowSorter<TableModel>(model);
int row = 0;
//arraylist to JTable
for(Person p:data)
{
model.setValueAt(p.lastName, row, 0);
model.setValueAt(p.firstName, row, 1);
model.setValueAt(p.email, row, 2);
model.setValueAt(p.dept, row, 3);
row++;
}
table.setModel(model);
table.setRowSorter(sorter);
表2示例代码
String[] colNames = {"Name","Email","Department","Status"};
model = new DefaultTableModel(colNames,500);
table.setModel(model);
try
{
conn = ac.getConnection();
stmt = conn.prepareStatement(insert);
rs = stmt.executeQuery();
int row = 0;
while(rs.next())
{
String[] rowData = new String[4];
for(int i=1;i<=4;i++)
{
rowData[i-1] = rs.getString(i);
}
//access database to jTable
model.setValueAt(rowData[0], row, 0);
model.setValueAt(rowData[1], row, 1);
model.setValueAt(rowData[2], row, 2);
model.setValueAt(rowData[3], row, 3);
row++;
}