2014-07-03 27 views
0

我有一個顯示在Jtable上的對象的數組列表。對於Jtable,我已經實現了一個擴展AbstractTableModel的模型。我希望用戶能夠保存並從.txt文件加載。現在,我可以創建並保存文件,但是當我嘗試將數據加載回jTable時,沒有任何反應。
請參閱我的代碼。任何建議表示讚賞!
謝謝!數據未加載回Jtable(使用可序列化對象)

模型

package cartedetelefon; 

import java.io.BufferedInputStream; 
import java.io.BufferedOutputStream; 
import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileNotFoundException; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.ObjectInput; 
import java.io.ObjectInputStream; 
import java.io.ObjectOutput; 
import java.io.ObjectOutputStream; 
import java.io.OutputStream; 
import java.io.Serializable; 
import java.util.ArrayList; 
import java.util.List; 
import java.util.logging.Level; 
import java.util.logging.Logger; 
import javax.swing.table.AbstractTableModel; 

public class CarteDeTelefon extends AbstractTableModel implements Serializable { 

    private static List<Abonat> listaContacte = new ArrayList<Abonat>(); 
    public static File f; 
    private static final long serialVersionUID = 1L; 
    private static final Logger fLogger = Logger.getLogger(CarteDeTelefon.class.getPackage().getName()); 

    /** 
    * @return the listaContacte 
    */ 
    public static List<Abonat> getListaContacte() { 
     return listaContacte; 
    } 

    /** 
    * @param aListaContacte the listaContacte to set 
    */ 
    public static void setListaContacte(List<Abonat> aListaContacte) { 
     listaContacte = aListaContacte; 
    } 

    private final String[] numeColoane = { 
     "Nume", 
     "Prenume", 
     "CNP", 
     "Numar telefon" 
    }; 

    // add contact to list 
    public void adaugareContact(String nume, String prenume, String cnp, String tel) { 

     try { 
      Long s = Long.valueOf(tel); 
      getListaContacte().add(new Abonat(nume, prenume, cnp, new NrTel(s))); 

      fireTableDataChanged(); 
     } catch (NumberFormatException numberFormatException) { 
     } 

    } 

    @Override 
    public int getRowCount() { 
     if (getListaContacte().size() <= 0) { 
      return 0; 
     } else { 
      return getListaContacte().size(); 
     } 

    } 

    @Override 
    public int getColumnCount() { 
     return numeColoane.length; 
    } 

    @Override 
    public String getColumnName(int col) { 
     return numeColoane[col]; 
    } 

    @Override 
    public Object getValueAt(int row, int col) { 
     if (col == 0) { 
      return getListaContacte().get(row).getNume(); 
     } else if (col == 1) { 
      return getListaContacte().get(row).getPrenume(); 
     } else if (col == 2) { 
      return getListaContacte().get(row).getCNP(); 
     } else if (col == 3) { 
      return getListaContacte().get(row).getNrTel(); 
     } 

     return "Eroare"; 
    } 

    @Override 
    public void setValueAt(Object aValue, int rowIndex, int colIndex) { 

     Abonat abonat = getListaContacte().get(rowIndex); 
     switch (colIndex) { 
      case 2: 
       abonat.setCNP((String) aValue); 
       break; 
      case 3: 
       abonat.setNrTel((NrTel) aValue); 
       break; 
     } 
     fireTableRowsUpdated(rowIndex, rowIndex); 

    } 

    @Override 
    public boolean isCellEditable(int row, int colNum) { 
     switch (colNum) { 
      case 2: 
       return false; 
      default: 
       return true; 
     } 
    } 

    //save contacts 
    public void salvareContacte() throws FileNotFoundException, IOException { 

     try (
       OutputStream file = new FileOutputStream("contacts.txt"); 
       OutputStream buffer = new BufferedOutputStream(file); 
       ObjectOutput output = new ObjectOutputStream(buffer);) { 
      output.writeObject(listaContacte); 
     } catch (IOException ex) { 
      fLogger.log(Level.SEVERE, "Cannot perform output.", ex); 
     } 

    } 

    //load contacts 
    public void incarcareContacte() throws IOException, ClassNotFoundException { 

     try (
       InputStream file = new FileInputStream("contacts.txt"); 
       InputStream buffer = new BufferedInputStream(file); 
       ObjectInput input = new ObjectInputStream(buffer);) { 
      //deserialize the List 
      List<Abonat> recoveredContacts = (ArrayList<Abonat>) input.readObject(); 
      //display its data 
      for (Abonat contacts : recoveredContacts) { 
       System.out.println("Recovered contacts: " + contacts); 
      } 
     } catch (ClassNotFoundException ex) { 
      fLogger.log(Level.SEVERE, "Cannot perform input. Class not found.", ex); 
     } catch (IOException ex) { 
      fLogger.log(Level.SEVERE, "Cannot perform input.", ex); 
     } 
    } 

} 

保存按鈕

private void saveActionPerformed(java.awt.event.ActionEvent evt) { 



try { 
    model.salvareContacte(); 
} catch (IOException ex) { 
    Logger.getLogger(GUI.class.getName()).log(Level.SEVERE, null, ex); 
} 

}

打開按鈕

private void openActionPerformed(java.awt.event.ActionEvent evt) { 



final JFileChooser fc = new JFileChooser(); 

if (evt.getSource() == open) { 
    int returnVal = fc.showOpenDialog(GUI.this); 

    if (returnVal == JFileChooser.APPROVE_OPTION) { 
     try { 
      try { 
       model.incarcareContacte(); 
      } catch (ClassNotFoundException ex) { 
       Logger.getLogger(GUI.class.getName()).log(Level.SEVERE, null, ex); 
      } 
     } catch (IOException ex) { 
      Logger.getLogger(GUI.class.getName()).log(Level.SEVERE, null, ex); 
     } 
    } 
} 

}

編輯:

加載文件,方法incarcareContacte顯示在控制檯上一條線,如後 「回收聯繫人:[email protected]」 爲每個條目

+0

你是什麼意思沒有任何反應?你的加載方法(incarcareContacte)甚至執行嗎?它是否會拋出異常?它是否反序列化一個空列表? –

+0

@馬克W看到編輯,請 – Zodrak

+0

在這種形式的問題很難回答正確,問題可以在模型通知或不能......,fireTableDataChanged()可以重新加載任何內容,但可以爲您的A炸彈代碼相同的方式 – mKorbel

回答

1

你永遠不會再將條目添加到模型中,請嘗試下面的代碼。您可能還需要重新驗證JTable以使其呈現新行。可能是對jtable.fireTableDataChanged()或類似的調用。

//load contacts 
public void incarcareContacte() throws IOException, ClassNotFoundException { 

    try (
      InputStream file = new FileInputStream("contacts.txt"); 
      InputStream buffer = new BufferedInputStream(file); 
      ObjectInput input = new ObjectInputStream(buffer);) { 
     //deserialize the List 
     List<Abonat> recoveredContacts = (ArrayList<Abonat>) input.readObject(); 
     //display its data 
     for (Abonat contacts : recoveredContacts) { 
      System.out.println("Recovered contacts: " + contacts); 
     } 
     listaContacte = recoveredContacts; 

    } catch (ClassNotFoundException ex) { 
     fLogger.log(Level.SEVERE, "Cannot perform input. Class not found.", ex); 
    } catch (IOException ex) { 
     fLogger.log(Level.SEVERE, "Cannot perform input.", ex); 
    } 
} 
+0

非常感謝。我錯過了!也錯過了fireTableDataChanged(); – Zodrak

+0

沒問題,謝謝你的回覆並接受答案。 –