我正在尝试序列化 abstractTableModel 并且当我尝试将表格内容保存在文件中时,该文件已成功保存,但我猜它始终为空(或未正确保存),因为它的大小始终为 17KB,无论我保存为空还是满桌...
这是我的约会安排表的 GUI:
http://i41.tinypic.com/2qm3p8k.jpg
这是我的代码......我希望这不是问题,因为它有更多的行......
public class TerminiPanel extends JFrame implements ActionListener, Serializable {
static JPanel terminPanel;
static Calendar terminiCalendar = new GregorianCalendar();
static int dan = terminiCalendar.get(Calendar.DAY_OF_MONTH);
static int mesec = terminiCalendar.get(Calendar.MONTH);
static int godina = terminiCalendar.get(Calendar.YEAR);
String mesecIme;
static String izabraniDan;
JPanel buttonPanel;
JScrollPane scrollPane;
JButton saveBtn;
static JTable table;
TerminiPanel(JPanel thisPanel) {
super();
this.terminPanel = thisPanel;
this.terminPanel.setLayout(new BorderLayout());
switch (Listener.monthChoose.getSelectedIndex()) {
case 0:
mesecIme = "Januar";
break;
case 1:
mesecIme = "Februar";
break;
case 2:
mesecIme = "Mart";
break;
case 3:
mesecIme = "April";
break;
case 4:
mesecIme = "Maj";
break;
case 5:
mesecIme = "Juni";
break;
case 6:
mesecIme = "Juli";
break;
case 7:
mesecIme = "Avgust";
break;
case 8:
mesecIme = "September";
break;
case 9:
mesecIme = "Oktobar";
break;
case 10:
mesecIme = "Novembar";
break;
default:
mesecIme = "Decembar";
break;
}
String tb = "Zakazani za " + dan + "." + mesecIme + " " + Listener.yearChoose.getSelectedItem();
TitledBorder titledBorder = new TitledBorder(tb);
terminPanel.setBorder(titledBorder);
///////////// Mali panel za dugme "Sacuvaj"
buttonPanel = new JPanel();
saveBtn = new JButton("Sacuvaj");
////////// Listener za zakazivanje - cuvanje podataka iz tabele u file /////////////
saveBtn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
TerminiPanel.main(terminPanel);
}
});
/////////////////////////////////////////////////////////////////////////////
buttonPanel.add(saveBtn);
DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
renderer.setHorizontalAlignment(JTextField.CENTER);
table = new JTable(new TerminiTableModel());
table.setRowHeight(25);
table.setCellSelectionEnabled(false);
table.setRowSelectionAllowed(false);
table.getColumnModel().getColumn(0).setMaxWidth(50);
table.getColumnModel().getColumn(0).setCellRenderer(renderer);
scrollPane = new JScrollPane(table);
terminPanel.add(buttonPanel, BorderLayout.SOUTH);
terminPanel.add(scrollPane, BorderLayout.CENTER);
}
///////////////////// prilikom klika na dan u kalendaru /////////////////////////
public void actionPerformed(ActionEvent e) {
izabraniDan = e.getActionCommand();
String tb = "Zakazani za " + izabraniDan + "." + mesecIme + " " + Listener.yearChoose.getSelectedItem();
TitledBorder titledBorder = new TitledBorder(tb);
terminPanel.setBorder(titledBorder);
TerminiTableModel tp = null;
try
{
System.out.println("Pocinjem ucitavanje fajla " + e.getActionCommand() + "" + (mesec + 1) + "" + Listener.yearChoose.getSelectedItem() + ".ser");
FileInputStream fileIn =
new FileInputStream("" + e.getActionCommand() + "" + (mesec + 1) + "" + Listener.yearChoose.getSelectedItem() + ".ser");
ObjectInputStream in = new ObjectInputStream(fileIn);
tp = (TerminiTableModel) in.readObject();
in.close();
fileIn.close();
System.out.println("Zavrsio . . . ");
}catch(IOException i) {
System.out.println("Nema fajla sa tim imenom . . . "); //ako nema fajla TO DO !!!!!!!!!!!!!!!!!!!
//i.printStackTrace();
return;
}catch(ClassNotFoundException c) {
System.out.println("TerminiPanel class not found");
//c.printStackTrace();
return;
}
///////////////////////////////////////////////////////////////////////////////////
}
public static void main(JPanel terminPanel2) {
TableModel tp = new TerminiTableModel().tableModel;
try {
if (izabraniDan == null)
izabraniDan = Integer.toString(dan);
FileOutputStream fileOut = new FileOutputStream("" + izabraniDan + "" + (mesec + 1) + "" + godina + ".ser");
ObjectOutputStream out = new ObjectOutputStream(fileOut);
out.writeObject(tp);
out.close();
fileOut.close();
JOptionPane.showMessageDialog(null, "Uspesno ste sacuvali zakazane pacijente za dan " + izabraniDan + "." + (mesec + 1) + "." + godina);
} catch (IOException ioex) {
ioex.printStackTrace();
}
}
}
我的 AbstractTableModel 在其他类中,如果需要,我也会上传它......我想知道我在哪里犯了错误,所以它不会保存我的表格内容,以及为什么点击后它甚至不加载空表在没有为自己保存文件的日期...
谢谢帮助...
PS:我编辑了代码......这是我所做的,现在没有错误,但它没有完成工作:-/