gpt4 book ai didi

java - 从数据库(通过 Netbeans)向 jTable 添加数据没有错误,但表消失了

转载 作者:行者123 更新时间:2023-11-29 03:24:20 25 4
gpt4 key购买 nike

在调试中,数据以某种方式被完全检索并且 resultModel 实际上已经获得了列名和行数据。尽管在搜索后在 Netbeans 中编译和运行时,表消失了,甚至列名也没有显示任何数据。这是代码:

private void search(){
String[][] rowData = new String[0][4];
String[] columns = {"appointmentid", "fname", "lname", "registration", "make", "model", "engine", "year", "mileage", "type", "date", "time"};
resultModel = new DefaultTableModel(rowData,columns);
add(new JScrollPane(jTable1));

jTable1 = new JTable(resultModel);

jTable1.setAutoCreateRowSorter(true);

try{
Model_Customer[] appointment = Controller_ManageCustomer.FindCustomers(Searchtxt.getText());
resultModel.setRowCount(0);
for(int i = 0; i < appointment.length; i++)
resultModel.insertRow(i,new Object[]{appointment[i].GetID(),appointment[i].GetFName(), appointment[i].GetLName(), appointment[i].GetRegistration(), appointment[i].GetMake(), appointment[i].GetModel(), appointment[i].GetEngine(), appointment[i].GetYear(), appointment[i].GetMileage(), appointment[i].GetType(), appointment[i].GetDate(), appointment[i].GetTime()});

}catch(Exception ex){
JOptionPane.showMessageDialog(this,ex.getMessage(),"Error",JOptionPane.ERROR_MESSAGE);
}

}
DefaultTableModel resultModel

最佳答案

可能只是我...但这看起来很可疑...

add(new JScrollPane(jTable1));
jTable1 = new JTable(resultModel);

更不用说,您构建了一个不错的新DefaultTableModel,但实际上并没有将它应用到屏幕上实际显示的任何内容...

尝试更像...

resultModel = new DefaultTableModel(rowData,columns);

try{
Model_Customer[] appointment = Controller_ManageCustomer.FindCustomers(Searchtxt.getText());
resultModel.setRowCount(0);
for(int i = 0; i < appointment.length; i++) {
resultModel.insertRow(i,new Object[]{appointment[i].GetID(),appointment[i].GetFName(), appointment[i].GetLName(), appointment[i].GetRegistration(), appointment[i].GetMake(), appointment[i].GetModel(), appointment[i].GetEngine(), appointment[i].GetYear(), appointment[i].GetMileage(), appointment[i].GetType(), appointment[i].GetDate(), appointment[i].GetTime()});
}

if (jTable1 == null) {
jTable1 = new JTable(resultModel);
add(new JScrollPane(jTable1));
} else {
jTable1.setModel(resultModel);
}

}catch(Exception ex){
JOptionPane.showMessageDialog(this,ex.getMessage(),"Error",JOptionPane.ERROR_MESSAGE);
}

现在,就我个人而言,我会简单地创建 JTable 并将其添加到屏幕上,然后不管它,只要在您想更新时更改 TableModel它的内容...

Swing 使用 MVC 的形式范式,这意味着它将 View 与模型分开,这意味着当您想要更改 JTable 显示的内容( View )时,您只需更改模型...

关于java - 从数据库(通过 Netbeans)向 jTable 添加数据没有错误,但表消失了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21844669/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com