// 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 Persist1X extends JFrame implements ActionListener {
    private final static String FILE_NAME = "C:\\p1x.dat";
    private JTextField tf1;
    private JTextField tf2;
    private JButton btn;
    public Persist1X() {
        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 save(String filename) {
        try {
            ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(filename));
            oos.writeObject(this);
            oos.close();
        } catch (FileNotFoundException ex) {
        } catch (IOException ex) {
        }
    }
    public static Persist1X load(String filename) {
        Persist1X p1x = null;
        try {
            ObjectInputStream ois = new ObjectInputStream(new FileInputStream(FILE_NAME));
            p1x = (Persist1X)ois.readObject();
            ois.close();
        } catch (FileNotFoundException e) {
        } catch (IOException e) {
        } catch (ClassNotFoundException e) {
        }
        return p1x;
    }
    public void actionPerformed(ActionEvent e) {
        save(FILE_NAME);
    }
    public static void main(String[] args) {
        Persist1X p1x;
        if((new File(FILE_NAME)).exists()) {
            p1x = load(FILE_NAME);
        } else {
            p1x = new Persist1X();
        }
        p1x.setVisible(true);
    }
}
