私はabstractTableModelをシリアル化しようとしていますが、テーブルのコンテンツをファイルに保存しようとすると、そのファイルは正常に保存されますが、空またはいっぱいのテーブル...
これが予定スケジュール表の私の 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:コードを編集しました...これが私がやったことです。今はエラーはありませんが、仕事はしません:-/