import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
public class Serialize2 extends JFrame {
private JPanel contentPane;
private JTable table;
private File tempFile;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Serialize2 frame = new Serialize2();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Serialize2() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
table = new JTable();
table.setRowHeight(30);
table.setModel(new DefaultTableModel(
new Object[][] {
{"Sepp",new Integer(1)},
{ "Fritz", 2},
{ "Karl", 3},
{ "Georg",4},
{ "Franz", 5},
},
new String[] {
"Name", "Bewertung"
}
) {
/**
*
*/
private static final long serialVersionUID = 1L;
Class[] columnTypes = new Class[] {
Object.class, String.class
};
public Class getColumnClass(int columnIndex) {
return columnTypes[columnIndex];
}
private void writeObject(ObjectOutputStream out) throws IOException
{
out.writeObject(this.columnIdentifiers);
out.writeObject(columnTypes);
out.writeObject(this.dataVector);
}
private void readObject(ObjectInputStream in) throws IOException
{
try{
Object o=in.readObject();
if(o instanceof Vector)
{
this.columnIdentifiers=(Vector<?>)o;
}
o=in.readObject();
if(o instanceof Class[])
{
this.columnTypes=(Class[])o;
}
o=in.readObject();
if(o instanceof Vector)
{
this.dataVector=(Vector<?>)o;
}
}catch(ClassNotFoundException e)
{
e.printStackTrace();
}
}
});
contentPane.add(table, BorderLayout.CENTER);
JPanel panel = new JPanel();
contentPane.add(panel, BorderLayout.SOUTH);
JButton btnSerialize = new JButton("Serialize");
btnSerialize.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0)
{
try {
tempFile=File.createTempFile("Table", "tmp");
ObjectOutputStream o=new ObjectOutputStream(new FileOutputStream(tempFile));
table.removeEditor();
o.writeObject(table.getModel());
o.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
panel.add(btnSerialize);
JButton btnDeserialize = new JButton("Deserialize");
btnDeserialize.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
if(tempFile==null)
{
JOptionPane.showMessageDialog((Component) e.getSource(), "Fehler","Erst serialisieren!",JOptionPane.ERROR_MESSAGE);
}
else
{
try {
ObjectInputStream in=new ObjectInputStream(new FileInputStream(tempFile));
TableModel tm=(TableModel)in.readObject();
table.setModel(tm);
table.revalidate();
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (ClassNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
});
panel.add(btnDeserialize);
}
}