Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Depending on the size of your application and the amount of data, Serializing the whole UI may be an option. </p> <p>It may be a bad idea though, when the information basically is retrieved and stored in a database already. In that case value objects and binding should be used, but for some simple applications where the UI is independent from another way of persisting you may use this.</p> <p>Of course, you can't modify the serialized values directly so, just consider this as an extra option:</p> <p><a href="http://img684.imageshack.us/img684/4581/capturadepantalla201001p.png" rel="nofollow noreferrer">alt text http://img684.imageshack.us/img684/4581/capturadepantalla201001p.png</a></p> <pre><code>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"); } }); } /** * Reads it serialized or create a new one if it doens't exists */ 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 ); } } </code></pre> <p>Here's the read/write as sketch, there is a lot of room for improvement here.</p> <pre><code> /** * write the object to a file */ 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 ){} } } /** * Read an object from the file */ 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 ){} } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload