How to remember the last values ​​in the form of a Swing GUI?

I have a simple Java GUI form created using Swing. It has some text inputs and checkboxes, and I want it to remember the last values ​​entered for them. Of course, you can save them to a file manually, and then read the file and fill in the inputs, but I wonder if there is a way to do this more or less automatically. Thanks

+6
java swing
source share
3 answers

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"); } }); } /** * 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 ); } } 

It is read / written here as a sketch, there are many opportunities for improvement.

  /** * 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 ){} } } } 
+2
source share

It is preferable to use the settings API .

Keeps preferences in the system, but this data is hidden from you - you focus on the structure and values ​​of your preferences, and not on implementation details (which depend on the platform).

This API also allows you to use different settings for different users on the same computer.

+5
source share

Not really. You must take care of saving the values ​​in a file or database.

0
source share

All Articles