Java – how do I remember the last value in the swing GUI form?

I have a simple java GUI form made with swing It has some text input and check boxes, and I want it to remember the last value entered into these inputs Of course, you can manually save them to a file and then read the file and fill in the input, but I wonder if there is a way to do this more or less automatically thank you

Solution

Depending on the size of the application and the amount of data, you can choose to serialize the entire UI

However, when the information is basically retrieved and stored in @ R_ 301_ 2416 @ this may be a bad idea In this case, you should use value objects and bindings, but for some simple applications, where the UI is independent of another persistence method, you can use it

Of course, you can't modify the serialized value directly, just think of it 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 );
        }
    }

Here is the read / write sketch. There is a lot of room 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 ){}
        }
    }
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>