Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

XML Encoding / Decoding and Swing

843805Feb 23 2007 — edited Feb 23 2007
Hi,

I was browsing the API�s this morning and I cam across the XMLEncoder and XMLDecoder. To my surprise I found out that the example used for the XMLEncoder class was serializing (is this the good word to use?) a JButton!

I was amazed because to my knowledge (which was wrong) GUI (Swing) elements could not be normally serialized (Binary Serialization). I created a quick example of this as follows:
 // imports.
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.XMLDecoder;
import java.beans.XMLEncoder;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;

// class.
public class SaveLoadView {

    public SaveLoadView() throws Exception{
        
        // frame1.
        JFrame frame1 = new JFrame();
        frame1.setLayout(new BorderLayout());
        
        // label.
        JLabel label = new JLabel("Hello1");
        label.setPreferredSize(new Dimension(200, 30));
        frame1.add(label, BorderLayout.CENTER);
        
        // button.
        JButton button = new JButton("Hello2");
        button.setPreferredSize(new Dimension(100, 30));
        button.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                System.out.println("Hello2");
            }
        });
        frame1.add(button, BorderLayout.SOUTH);
        
        // frame1 properties.
        frame1.setTitle("Save/Load View");
        frame1.setSize(200,100);
        frame1.pack();
        frame1.setVisible(true);
        
        // save frame1.
        this.save("C:\\frame.xml", frame1);
        
        // load frame1 (as frame2).
        JFrame frame2 = (JFrame)this.load("C:\\frame.xml");
        frame2.setVisible(true);
    }
    
    // load.
    public JFrame load(String file) throws IOException{
        
        XMLDecoder decoder = null;
        JFrame frame = null;

        try{
            decoder = new XMLDecoder(new FileInputStream(file));
            Object o = decoder.readObject();
            frame = (JFrame)o;
        }finally{
            decoder.close();
        }

        return frame;
    }

    // save.
    public void save(String file, JFrame frame) throws IOException {
        XMLEncoder encoder = null;

        try{
            encoder = new XMLEncoder(new FileOutputStream(file));
            encoder.writeObject(frame);
        }
        catch(Exception e){
            System.out.println(e.toString());
           }
        finally{
            encoder.close();
        }
    }

    // main.
    public static void main(String[] args) throws Exception {
        
        new SaveLoadView();
    } 
}
This code worked fine and created me the following XML
 <?xml version="1.0" encoding="UTF-8"?> 
<java version="1.5.0_02" class="java.beans.XMLDecoder"> 
 <object class="javax.swing.JFrame"> 
  <void property="size"> 
   <object class="java.awt.Dimension"> 
    <int>208</int> 
    <int>87</int> 
   </object> 
  </void> 
  <void property="contentPane"> 
   <void method="add"> 
    <object id="JLabel0" class="javax.swing.JLabel"> 
     <void property="text"> 
      <string>Hello1</string> 
     </void> 
    </object> 
   </void> 
   <void method="add"> 
    <object id="JButton0" class="javax.swing.JButton"> 
     <string>Hello2</string> 
    </object> 
   </void> 
   <void property="layout"> 
    <object class="java.awt.BorderLayout"> 
     <void method="addLayoutComponent"> 
      <object idref="JButton0"/> 
      <string>South</string> 
     </void> 
     <void method="addLayoutComponent"> 
      <object idref="JLabel0"/> 
      <string>Center</string> 
     </void> 
    </object> 
   </void> 
  </void> 
  <void property="name"> 
   <string>frame0</string> 
  </void> 
  <void property="title"> 
   <string>Save/Load View</string> 
  </void> 
  <void property="visible"> 
   <boolean>true</boolean> 
  </void> 
 </object> 
</java>
However I realized that it did not encode the event as well! I can load the Jframe and show it, but the button will not have any action!

I was wondering; Is there something or some code that also encode the events of a class and also the methods??

Regards,
Sim085
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Mar 23 2007
Added on Feb 23 2007
3 comments
260 views