// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
import java.awt.*;
import java.awt.event.*;
import java.io.*;

import javax.swing.*;

public class Persist1 extends JFrame implements ActionListener {
    private final static String FILE_NAME = "C:\\p1.dat";
    private JTextField tf1;
    private JTextField tf2;
    private JButton btn;
    public Persist1() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        getContentPane().setLayout(new GridLayout(3, 1));
        tf1 = new JTextField();
        getContentPane().add(tf1);
        tf2 = new JTextField();
        getContentPane().add(tf2);
        btn = new JButton("Save");
        btn.addActionListener(this);
        getContentPane().add(btn);
        pack();
    }
    public void actionPerformed(ActionEvent e) {
        try {
            ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(FILE_NAME));
            oos.writeObject(this);
            oos.close();
        } catch (FileNotFoundException ex) {
        } catch (IOException ex) {
        }
    }
    public static void main(String[] args) {
        Persist1 p1 = null;
        if((new File(FILE_NAME)).exists()) {
            try {
                ObjectInputStream ois = new ObjectInputStream(new FileInputStream(FILE_NAME));
                p1 = (Persist1)ois.readObject();
                ois.close();
            } catch (FileNotFoundException e) {
            } catch (IOException e) {
            } catch (ClassNotFoundException e) {
            }
        } else {
            p1 = new Persist1();
        }
        p1.setVisible(true);
    }
}
