Display all deserialized Objects in JTable in JAVA

janetsnail

New Member
i want to show all objects in a table.i have a folder named /menschen which contains files like this: "firstname.lastname.ser"\[code\]package adressverwaltung;import java.awt.Color;import java.awt.FlowLayout;import java.awt.GridLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.JFrame;import javax.swing.JPanel;import javax.swing.JLabel;import javax.swing.JButton;import javax.swing.JOptionPane;// FILEimport java.io.*;import java.util.*;import java.util.Formatter;import java.util.logging.Level;import java.util.logging.Logger;import java.awt.HeadlessException;import javax.swing.BorderFactory;import javax.swing.BoxLayout;import javax.swing.JScrollPane;import javax.swing.JTable;import javax.swing.JTextField;import javax.swing.SpringLayout;/** * * @author */public class Adressverwaltung { JFrame mainWindow; final File folder = new File("menschen"); /** * @param args the command line arguments */ public static void main(String[] args) { Adressverwaltung verweiss = new Adressverwaltung(); verweiss.main(); } public void main() { mainWindow = new JFrame(); mainWindow.setBounds(0, 0, 800, 400); mainWindow.setLocationRelativeTo(null); mainWindow.setLayout(null); mainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); mainWindow.setResizable(false); mainWindow.setVisible(true); mainWindow.setLayout(new FlowLayout()); menu(); } public String deserialize(String m, int field) { try { FileInputStream fin = new FileInputStream("menschen\\" + m); ObjectInputStream ois = new ObjectInputStream(fin); Person n = (Person) ois.readObject(); ois.close(); switch (field) { case 1: return n.vorname; case 2: return n.nachname; } } catch (Exception ex) { ex.printStackTrace(); } return null; } public static void table(String[][] alle) { String[] columnNames = { "Vorname", "Nachname" }; JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JTable table = new JTable(alle, columnNames); f.add(new JScrollPane(table)); f.pack(); f.setVisible(true); } private static void serialize(Person m) { try { FileOutputStream fileOut = new FileOutputStream("menschen\\" + m.vorname + "." + m.nachname + ".ser"); ObjectOutputStream out = new ObjectOutputStream(fileOut); out.writeObject(m); out.close(); fileOut.close(); } catch (IOException i) { } } public void menu() { JPanel list = new JPanel(); list.setBorder(BorderFactory.createLineBorder(Color.black)); list.setPreferredSize(new java.awt.Dimension(400, 360)); list.setBackground(Color.white); mainWindow.add(list); // Wir lassen unseren Dialog anzeigen mainWindow.setVisible(true); int ButtonWidth = 100; int ButtonHeight = 30; int ButtonTop = 10; JButton Button1 = new JButton("List all"); JButton Button3 = new JButton("Search"); Button1.setBounds(10, ButtonTop, ButtonWidth, ButtonHeight); Button3.setBounds(230, ButtonTop, ButtonWidth, ButtonHeight); list.add(Button1); list.add(Button3); mainWindow.add(list); createForm(); Button1.addActionListener(new ActionListener() { public void actionPerformed(final ActionEvent e) { list(folder); } }); } public void list(final File folder) { String[][] rowData = http://stackoverflow.com/questions/15724243/new String[3][]; int r = 0; for (final File fileEntry : folder.listFiles()) { if (fileEntry.isDirectory()) { } else { String name = fileEntry.getName(); rowData[r][0] = deserialize(name, 1); rowData[r][1] = deserialize(name, 2); r++; } } table(rowData); } private void createForm() { JPanel p = new JPanel(); p.setLayout(new GridLayout(3, 2)); JButton b = new JButton("Neue Person!"); JLabel vornameLabel = new JLabel("Vorname:"); final JTextField vorname = new JTextField(); JLabel nachnameLabel = new JLabel("Nachname:"); final JTextField nachname = new JTextField(); p.add(vornameLabel); p.add(vorname); p.add(nachnameLabel); p.add(nachname); p.add(b); p.setBorder(BorderFactory.createLineBorder(Color.black)); p.setPreferredSize(new java.awt.Dimension(300, 100)); p.setBackground(Color.white); mainWindow.add(p); // Wir lassen unseren Dialog anzeigen mainWindow.setVisible(true); b.addActionListener(new ActionListener() { public void actionPerformed(final ActionEvent e) { Person m = new Person(vorname.getText(), nachname.getText()); serialize(m); JOptionPane.showMessageDialog(null, vorname.getText() + "." + nachname.getText() + ".ser abgespeichert.", "Tutorial 2", JOptionPane.INFORMATION_MESSAGE); } }); }}\[/code\]And this is my Person class:\[code\]package adressverwaltung;/** * * @author Mahshid */class Person implements java.io.Serializable { // Allgemeine Kontaktinformationen: public String vorname; public String nachname; // Adresse public String strasse; public int hausnummer; public int postleitzahl; public String ort; public String land; // Telefon public int mobil; public int festnetz; // Email & Kommentar public String mail; public String kommentar; Person(String vorname, String nachname) { this.vorname = vorname; this.nachname = nachname; }}\[/code\]I think there is a problem in list() function. this is my first java code so please don't be surpriced if you see any big mistakes :)
 
Back
Top