This code helps to save and load the Java GUI in local disk using Javabeans persistence Mechanism
import java.awt.*; import java.awt.event.*; import java.beans.*; import java.io.*; import javax.swing.*; /** * Java code to save and load a GUI to local disk */ public class GUIPersistence { private static JFileChooser chooser; private JFrame frame; Registry re; public static void main(String[] args){ chooser = new JFileChooser(); chooser.setCurrentDirectory(new File(".")); GUIPersistence persist = new GUIPersistence(); persist.init(); } public void init(){ frame = new JFrame(); frame.setLayout(new FlowLayout()); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setTitle("GUI PersistentFrame"); frame.setSize(400,200); JButton registryButton = new JButton("Registry"); frame.add(registryButton); registryButton.addActionListener(EventHandler.create(ActionListener.class, this, "registry")); JButton saveButton = new JButton("Save"); frame.add(saveButton); saveButton.addActionListener(EventHandler.create(ActionListener.class, this, "save")); JButton loadButton = new JButton("Load"); frame.add(loadButton); loadButton.addActionListener(EventHandler.create(ActionListener.class, this, "load")); frame.setVisible(true); } public void registry(){ re = new Registry(); //re.getFrame(); } /* * Method to save the frame */ public void save() { if(chooser.showSaveDialog(null)==JFileChooser.APPROVE_OPTION) { try{ File file = chooser.getSelectedFile(); XMLEncoder encoder = new XMLEncoder(new FileOutputStream(file)); encoder.writeObject(frame); if (re.getFrame() != null) { encoder.writeObject(re.getFrame()); } encoder.close(); } catch(IOException e) { JOptionPane.showMessageDialog(null, e); } } } /* * Method to load the frame */ public void load() { //show file chooser dialog int r = chooser.showOpenDialog(null); // if file selected, open if(r == JFileChooser.APPROVE_OPTION) { try { File file = chooser.getSelectedFile(); XMLDecoder decoder = new XMLDecoder(new FileInputStream(file)); decoder.readObject(); decoder.close(); } catch(IOException e) { JOptionPane.showMessageDialog(null, e); } } } } public class Registry { JFrame frame; public Registry(){ frame = new JFrame(); frame.setLayout(new FlowLayout()); frame.setSize(400,200); JLabel nameL = new JLabel("Name:"); JTextField nameF = new JTextField(8); frame.add(nameL); frame.add(nameF); frame.setVisible(true); } public JFrame getFrame(){ return frame; } }