tags:

views:

67

answers:

3

Hello. I have a simple Java GUI form made with Swing. It has some text inputs and checkboxes and I want it to remember the last values input to these. Of course it is possible to save them to some file manually and then read the file and fill the inputs, but I wonder if there's a way to do this more or less automatically. Thanks

A: 

Not really. You have to take care of persisting the values to a file or database yourself.

Paul Tomblin
+3  A: 

It is preferred to use the Preferences API.

It stores the preferences in the system, but this details are hidden from you - you focus on the structure and values of your preferences, rather than the implementation details (which are platform specific).

This API also allows for different settings for different users on the same machine.

Bozho
Gonna give it a try
roddik
+2  A: 

Depending on the size of your application and the amount of data, Serializing the whole UI may be an option.

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.

Of course, you can't modify the serialized values directly so, just consider this as an extra option:

alt text

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 );
        }
    }

Here's the read/write as sketch, there is a lot of room for improvement here.

    /**
     * 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 ){}
        }
    }
} 
OscarRyz