Depending on the size of your application and the amount of data, the option Serialize the entire user interface may be selected.
Perhaps this is a bad idea when information is mostly extracted and stored in the database already. In this case, the values ββof the objects and bindings should be used, but for some simple applications where the user interface does not depend on another way of saving, you can use this.
Of course, you cannot directly modify serialized values, just consider this as an additional option:
alt text http://img684.imageshack.us/img684/4581/capturadepantalla201001p.png
import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.io.*; public class SwingTest { public static void main( String [] args ) { final JFrame frame = getFrame(); frame.pack(); frame.setVisible( true ); Runtime.getRuntime().addShutdownHook(new Thread() { public void run() { writeToFile( frame, "swingtest.ser"); } }); } private static JFrame getFrame(){ File file = new File("swingtest.ser"); if( !file.exists() ) { System.out.println("creating a new one"); JFrame frame = new JFrame(); JPanel panel = new JPanel(); panel.add( new JLabel("Some test here:")); panel.add( new JTextField(10)); frame.add( panel ); return frame; } else { return ( JFrame ) readObjectFrom( file ); } }
It is read / written here as a sketch, there are many opportunities for improvement.
private static void writeToFile( Serializable s , String fileName ) { ObjectOutputStream oos = null; try { oos = new ObjectOutputStream( new FileOutputStream( new File( fileName ))); oos.writeObject( s ); } catch( IOException ioe ){ } finally { if( oos != null ) try { oos.close(); } catch( IOException ioe ){} } } private static Object readObjectFrom( File f ) { ObjectInputStream ois = null; try { ois = new ObjectInputStream( new FileInputStream( f )) ; return ois.readObject(); } catch( ClassNotFoundException cnfe ){ return null; } catch( IOException ioe ) { return null; } finally { if( ois != null ) try { ois.close(); } catch( IOException ioe ){} } } }
OscarRyz
source share