This program demonstrates the use of an XML encoder and decoder to save and restore a frame
/*
This program is a part of the companion code for Core Java 8th ed.
(http://horstmann.com/corejava)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see .
*/
import java.awt.*;
import java.awt.event.*;
import java.beans.*;
import java.io.*;
import javax.swing.*;
/**
* This program demonstrates the use of an XML encoder and decoder to save and restore a frame.
* @version 1.01 2007-10-03
* @author Cay Horstmann
*/
public class PersistentFrameTest
{
public static void main(String[] args)
{
chooser = new JFileChooser();
chooser.setCurrentDirectory(new File("."));
PersistentFrameTest test = new PersistentFrameTest();
test.init();
}
public void init()
{
frame = new JFrame();
frame.setLayout(new FlowLayout());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("PersistentFrameTest");
frame.setSize(400, 200);
JButton loadButton = new JButton("Load");
frame.add(loadButton);
loadButton.addActionListener(EventHandler.create(ActionListener.class, this, "load"));
JButton saveButton = new JButton("Save");
frame.add(saveButton);
saveButton.addActionListener(EventHandler.create(ActionListener.class, this, "save"));
frame.setVisible(true);
}
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 void save()
{
if (chooser.showSaveDialog(null) == JFileChooser.APPROVE_OPTION)
{
try
{
File file = chooser.getSelectedFile();
XMLEncoder encoder = new XMLEncoder(new FileOutputStream(file));
encoder.writeObject(frame);
encoder.close();
}
catch (IOException e)
{
JOptionPane.showMessageDialog(null, e);
}
}
}
private static JFileChooser chooser;
private JFrame frame;
}