Results 1 to 5 of 5
  1. #1
    Dennis's Avatar
    Dennis is offline Member
    Join Date
    May 2010
    Posts
    83
    Rep Power
    0

    Default Serialize ActionListener

    Hi everyone,

    I can write a java.awt.Panel into an file with an ObjectOutputStream and read it back with an ObjectInputStream.

    Now in the Panel is an (java.awt.)Button and I want to put an ActionListener on the Button.

    But an ActionListener is not Serializable.

    Does someone know how I can serialize an ActionListener to a file?

    Thanks,
    Dennis

    Writer:
    Java Code:
    import java.awt.Button;
    import java.awt.Color;
    import java.awt.Panel;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.ObjectOutputStream;
    
    public class panelSerializableTestWrite
    {
    	public panelSerializableTestWrite()
    	{		
    		try {
    			ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(new File("bin/aap.txt")));
    			oos.writeObject(getP());
    			oos.close();
    		} catch (FileNotFoundException e) {
    			e.printStackTrace();
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    	}
    	
    	private Panel getP()
    	{
    		Panel p = new Panel();
    		p.setBackground(Color.black);
    		p.setLayout(null);
    		Button loginButton = new Button("Click me!");
    		loginButton.addActionListener(new ActionListener(){
    			@Override
    			public void actionPerformed(ActionEvent e)
    			{
    				System.out.println("Clicked");
    			}
    		});
    		loginButton.setBounds(0, 0, 200, 30);
    		p.add(loginButton);
    		return p;
    	}
    	
    	public static void main(String... args)
    	{
    		new panelSerializableTestWrite();
    	}
    }
    Reader:
    Java Code:
    import java.awt.Panel;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.io.ObjectInputStream;
    
    public class panelSerializableTest extends java.applet.Applet
    {
    	private static final long serialVersionUID = 1L;
    
    	public void init()
    	{
    		setLayout(null);
    
    		try {
    			File f = new File("file.txt");
    			ObjectInputStream ois = new ObjectInputStream(new FileInputStream(f));
    			Object o = ois.readObject();
    			if(o instanceof Panel){
    				Panel p = (Panel)o;
    				p.setBounds(0, 0, 800, 500);
    				add(p);
    			}
    		} catch (FileNotFoundException e) {
    			e.printStackTrace();
    		} catch (IOException e) {
    			e.printStackTrace();
    		} catch (ClassNotFoundException e) {
    			e.printStackTrace();
    		}
    	}
    }

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,953
    Rep Power
    19

    Default

    You can't, because it's not serializable.

    Why do you want to serialize a Panel in the first place?
    Surely you can define the thing the Panel is showing as some data object that the Panel uses to build itself?

  3. #3
    Dennis's Avatar
    Dennis is offline Member
    Join Date
    May 2010
    Posts
    83
    Rep Power
    0

    Default

    You mean I have to send the Button only, for putting that one the reader side at the panel?

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Dennis View Post
    You mean I have to send the Button only, for putting that one the reader side at the panel?
    No, I don't think he means that. Instead of trying to serialize the GUI, you should serialize the state of the logic that the GUI is displaying. So for instance if your GUI is displaying a card game, then you should use serialization to store the non-GUI information such as the cards held in the deck, the cards held by each player, the points each player has, etc. You wouldn't want to serialize the buttons, labels, and graphics, but could easily rebuild the GUI from the data that you've saved by serialization.

    One other thing to note though it doesn't directly apply to your problem, if you want to serialize an object but it holds some fields that can't be serialized, you can declare them as transient and the serialization manager will not try to serialize them.

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,953
    Rep Power
    19

    Default

    Yes, that's what I'm saying.
    I see a lot of questions around serialising GUIs here (and other forums) when what is really needed is that the state is what should actually be serialised.

    However, depending on what it is you're writing, serialisation may not be the best thing to use in any case. If it's something intended to be stored and reread over anything other than a short period of time then you run the risk of problems should you modify any of the serialised classes. Upgrading would be fun.

Similar Threads

  1. Using JSTL to capture HTTP referer data and serialize into a variable
    By jeremy.wilson in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 05-19-2011, 12:44 PM
  2. Serialize array in java and unserialize in php
    By chintan.vyas89 in forum New To Java
    Replies: 2
    Last Post: 01-09-2011, 10:54 AM
  3. Serialize this!
    By dustybray in forum Advanced Java
    Replies: 1
    Last Post: 12-03-2010, 01:31 AM
  4. To serialize or not to serialize?
    By XmisterIS in forum New To Java
    Replies: 2
    Last Post: 09-21-2010, 01:37 PM
  5. serialize to web service?
    By theartz in forum Advanced Java
    Replies: 2
    Last Post: 08-16-2008, 01:39 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •