以某种方式调试数据被完全检索,并且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
答案 0 :(得分:4)
可能只是我......但这看起来很可疑......
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
,只要您想要更新它的内容...... < / p>
Swing使用MVC范例的形式,这意味着它将视图与模型分开,这意味着当您想要更改JTable
所显示的内容(视图)时,您只需更改模特......