This program demonstrates the use of an XML encoder and decoder to save and restore a frame. : Java Beans « Development Class « Java






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 <http://www.gnu.org/licenses/>.
*/

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;
}



<?xml version="1.0" encoding="UTF-8"?> 
<java version="1.6.0_01" class="java.beans.XMLDecoder"> 
 <object class="javax.swing.JFrame"> 
  <void property="bounds"> 
   <object class="java.awt.Rectangle"> 
    <int>33</int> 
    <int>0</int> 
    <int>400</int> 
    <int>200</int> 
   </object> 
  </void> 
  <void property="contentPane"> 
   <void method="add"> 
    <object class="javax.swing.JButton"> 
     <string>Load</string> 
     <void method="addActionListener"> 
      <object class="java.beans.EventHandler" method="create"> 
       <class>java.awt.event.ActionListener</class> 
       <object id="PersistentFrameTest0" class="PersistentFrameTest"/> 
       <string>load</string> 
      </object> 
     </void> 
    </object> 
   </void> 
   <void method="add"> 
    <object class="javax.swing.JButton"> 
     <string>Save</string> 
     <void property="model"> 
      <void property="rollover"> 
       <boolean>true</boolean> 
      </void> 
     </void> 
     <void method="addActionListener"> 
      <object class="java.beans.EventHandler" method="create"> 
       <class>java.awt.event.ActionListener</class> 
       <object idref="PersistentFrameTest0"/> 
       <string>save</string> 
      </object> 
     </void> 
    </object> 
   </void> 
   <void property="layout"> 
    <object class="java.awt.FlowLayout"/> 
   </void> 
  </void> 
  <void property="defaultCloseOperation"> 
   <int>3</int> 
  </void> 
  <void property="minimumSize"> 
   <object class="java.awt.Dimension"> 
    <int>143</int> 
    <int>62</int> 
   </object> 
  </void> 
  <void property="name"> 
   <string>frame0</string> 
  </void> 
  <void property="title"> 
   <string>PersistentFrameTest</string> 
  </void> 
  <void method="addWindowListener"> 
   <object class="sun.awt.im.InputMethodContext"/> 
  </void> 
  <void property="visible"> 
   <boolean>true</boolean> 
  </void> 
 </object> 
</java> 

   
    
  








Related examples in the same category

1.JavaBean: BeanContextSupportJavaBean: BeanContextSupport
2.JavaBean: Test program that adds 100 beans to a contextJavaBean: Test program that adds 100 beans to a context
3.JavaBean: how to use the instantiateChild() convenience method to create a beanJavaBean: how to use the instantiateChild() convenience method to create a bean
4.JavaBean: illustrate delivery of the BeanContextMembershipEventJavaBean: illustrate delivery of the BeanContextMembershipEvent
5.JavaBean: creates all of the objects, a tests the service capabilitiesJavaBean: creates all of the objects, a tests the service capabilities
6.Bean ContainerBean Container
7.PropertyTablePropertyTable
8.Introspecting a BeanIntrospecting a Bean
9.Listening for Changes to the Selected File in a JFileChooser Dialog
10.Get a list of selected files
11.Listening for Changes to the Current Directory in a JFileChooser Dialog
12.Displaying the Current Directory in the Title of a JFileChooser Dialog
13.Setting an Accessory Component in a JFileChooser Dialog
14.Convert a bean to XML persistence
15.Listen for bean's property change event
16.List property names of a Bean
17.Prevent bean's property being serialized to XML
18.Create an instance a Bean
19.Convert an XML persistence to bean
20.Determine bean's property type
21.Listen for a constrained property change
22.Bean has a single property called property.
23.Implementing a Bound Property
24.Implementing a Constrained Property: fires a PropertyChangeEvent whenever its value is about to be changed.
25.Instantiating a Bean
26.Listing the Property Names of a Bean
27.Getting and Setting a Property of a Bean
28.Get and set the value of a property in a bean using Expression and Statement
29.Get and set an Object type property
30.gets and sets a primitive type property
31.gets and sets an array type property
32.Serializing a Bean to XML: XMLEncoder only persists the value of public properties.
33.Deserializing a Bean from XML
34.Preventing a Bean Property from Being Serialized to XML
35.Serializing an Immutable Bean Property to XML
36.Listening for a Property Change Event: A property change event is fired when a bound property is changed.
37.Listening for a Vetoable Property Change Event
38.Read bean's property value
39.Saving and restoring the state of a pretend CAD systemSaving and restoring the state of a pretend CAD system
40.An extension of ArrayList that provides some handy utilities for working with JavaBeans
41.An extension of Vector that provides some handy utilities for working with JavaBeans
42.extends SimpleBeanInfo
43.Get and set properties on a bean
44.Bean Utility
45.Is JavaBean Compliant Setter
46.This program demonstrates various persistence delegates.
47.Event Tracer