序列化AbstractTableModel

时间:2013-06-05 08:28:19

标签: java swing serialization jtable

我正在尝试序列化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:我编辑了代码......这就是我所做的,现在没有错误,但它不能完成这项工作: - /

0 个答案:

没有答案