我创建了一个JTable来显示从SQL数据库中提取的约会记录。最初,表中填充了正确的数据,这意味着代码可以正常工作,但是当我激活刷新方法时,表不会更新。可以通过JPanel嵌套来实现吗?该表位于:JFrame> JPanel> JPanel> JTable中。
代码如下:
public DisplayApps(ConnectHub sqldatabase, MainPanel mp){
this.columns = new String[]{"Type", "Appointment ID", "Patient ID", "Patient ID 2", "Room ID", "Doctor ID", "Scheduled By ID", "Date", "Time", "Length", "Has Happened", "Summary", "Date Created"};
this.sqldatabase = sqldatabase;
this.mp = mp;
GridLayout mainlayout = new GridLayout(1,0);
this.setLayout(mainlayout);
table = new JTable();
table.setModel(new DefaultTableModel(new Object[][]{},columns));
refresh();
table.setPreferredScrollableViewportSize(new Dimension(1000, 70));
table.setFillsViewportHeight(true);
scrollPane = new JScrollPane(table);
add(scrollPane);
}
public Object getRowIndex(){
int row = table.getSelectedRow();
if(row==-1) return -1;
return table.getValueAt(row,1);
}
public void refresh(){
DefaultTableModel model = (DefaultTableModel) table.getModel();
model.setRowCount(0);
appointments = sqldatabase.getAllAppointments();
for(int i=0;i<appointments.size();i++){
Object[] data = new Object[13];
Appointment temp = appointments.get(i);
boolean routine = true;
if(temp instanceof RoutineAppointment)
data[0] = "Routine";
else{
data[0] = "Care";
routine = false;
}
data[1] = appointments.get(i).getAppID();
if(routine){
data[2] = ((RoutineAppointment)appointments.get(i)).getPatientID();
data[3] = "NA";
}
else{
data[2] = ((CareAppointment)appointments.get(i)).getPatientID1();
data[3] = ((CareAppointment)appointments.get(i)).getPatientID2();
}
if(routine)
data[4] = "NA";
else
data[4] = ((CareAppointment)appointments.get(i)).getRoomID();
data[5] = appointments.get(i).getDoctorID();
data[6] = appointments.get(i).getScheduledByID();
data[7] = appointments.get(i).getDate();
data[8] = appointments.get(i).getTime();
data[9] = appointments.get(i).getLength();
data[10] = appointments.get(i).isHasHappened();
data[11] = appointments.get(i).getSummary();
data[12] = appointments.get(i).getDateCreated().toString();
model.addRow(data);
}
table.revalidate();
table.repaint();
}