Y
yancheng.cheok
In my application, I perform serialization on my JTable's TableModel.
I realize that, even I didn't make any modification (either save or
delete data) on the JTable content, however, everytime I save the
JTable's TableModel, the serialization file will getting bigger and
bigger (about 2KB)
It works this way
1. I shut down my application. My application save JTable's TableModel
to serialization file. The file size is 50 KB
2. I open up my application. My application load JTable's TableModel.
Everything in the JTable content restore.
3. I shut down my application again, without modification on the
JTable's TableModel. The file size is 52 KB.
4. If I keep repeating step 1, the file size will keep increase.
May I know how I can prevent my serialization file from getting bigger
and bigger?
Thanks
--------code----------
/*
* NewJFrame.java
*
* Created on April 8, 2007, 11:40 PM
*/
import java.io.*;
import javax.swing.table.*;
/**
*
* @author doraemon
*/
public class NewJFrame extends javax.swing.JFrame {
/** Creates new form NewJFrame */
public NewJFrame() {
initComponents();
loadTableModel();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
// <editor-fold defaultstate="collapsed" desc=" Generated Code
">
private void initComponents() {
jScrollPane1 = new javax.swing.JScrollPane();
jTable1 = new javax.swing.JTable();
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosed(java.awt.event.WindowEvent evt) {
formWindowClosed(evt);
}
});
jTable1.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null}
},
new String [] {
"Title 1", "Title 2", "Title 3", "Title 4"
}
));
jScrollPane1.setViewportView(jTable1);
javax.swing.GroupLayout layout = new
javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane1,
javax.swing.GroupLayout.PREFERRED_SIZE, 375,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(15, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jScrollPane1,
javax.swing.GroupLayout.PREFERRED_SIZE, 275,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(25, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void formWindowClosed(java.awt.event.WindowEvent evt)
{
// TODO add your handling code here:
saveTableModel();
System.out.println("table model saved");
}
private boolean saveTableModel() {
try {
FileOutputStream fos = new
FileOutputStream("table.ser");
ObjectOutputStream out = new ObjectOutputStream(fos);
out.writeObject(jTable1.getModel());
out.close();
}
catch(IOException exp) {
exp.printStackTrace();
return false;
}
return true;
}
private boolean loadTableModel() {
TableModel tableModel = null;
try {
FileInputStream fos = new FileInputStream("table.ser");
ObjectInputStream in = new ObjectInputStream(fos);
tableModel = (TableModel)in.readObject();
in.close();
}
catch(IOException exp) {
exp.printStackTrace();
return false;
}
catch(ClassNotFoundException exp) {
exp.printStackTrace();
return false;
}
if(tableModel != null)
jTable1.setModel(tableModel);
return true;
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new NewJFrame().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable jTable1;
// End of variables declaration
}
I realize that, even I didn't make any modification (either save or
delete data) on the JTable content, however, everytime I save the
JTable's TableModel, the serialization file will getting bigger and
bigger (about 2KB)
It works this way
1. I shut down my application. My application save JTable's TableModel
to serialization file. The file size is 50 KB
2. I open up my application. My application load JTable's TableModel.
Everything in the JTable content restore.
3. I shut down my application again, without modification on the
JTable's TableModel. The file size is 52 KB.
4. If I keep repeating step 1, the file size will keep increase.
May I know how I can prevent my serialization file from getting bigger
and bigger?
Thanks
--------code----------
/*
* NewJFrame.java
*
* Created on April 8, 2007, 11:40 PM
*/
import java.io.*;
import javax.swing.table.*;
/**
*
* @author doraemon
*/
public class NewJFrame extends javax.swing.JFrame {
/** Creates new form NewJFrame */
public NewJFrame() {
initComponents();
loadTableModel();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
// <editor-fold defaultstate="collapsed" desc=" Generated Code
">
private void initComponents() {
jScrollPane1 = new javax.swing.JScrollPane();
jTable1 = new javax.swing.JTable();
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosed(java.awt.event.WindowEvent evt) {
formWindowClosed(evt);
}
});
jTable1.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null}
},
new String [] {
"Title 1", "Title 2", "Title 3", "Title 4"
}
));
jScrollPane1.setViewportView(jTable1);
javax.swing.GroupLayout layout = new
javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane1,
javax.swing.GroupLayout.PREFERRED_SIZE, 375,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(15, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jScrollPane1,
javax.swing.GroupLayout.PREFERRED_SIZE, 275,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(25, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void formWindowClosed(java.awt.event.WindowEvent evt)
{
// TODO add your handling code here:
saveTableModel();
System.out.println("table model saved");
}
private boolean saveTableModel() {
try {
FileOutputStream fos = new
FileOutputStream("table.ser");
ObjectOutputStream out = new ObjectOutputStream(fos);
out.writeObject(jTable1.getModel());
out.close();
}
catch(IOException exp) {
exp.printStackTrace();
return false;
}
return true;
}
private boolean loadTableModel() {
TableModel tableModel = null;
try {
FileInputStream fos = new FileInputStream("table.ser");
ObjectInputStream in = new ObjectInputStream(fos);
tableModel = (TableModel)in.readObject();
in.close();
}
catch(IOException exp) {
exp.printStackTrace();
return false;
}
catch(ClassNotFoundException exp) {
exp.printStackTrace();
return false;
}
if(tableModel != null)
jTable1.setModel(tableModel);
return true;
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new NewJFrame().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable jTable1;
// End of variables declaration
}