Results 1 to 5 of 5
Thread: Serialize ActionListener
- 01-20-2011, 04:09 PM #1
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:
Reader: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(); } }
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(); } } }
- 01-20-2011, 04:54 PM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
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?
- 01-20-2011, 05:40 PM #3
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.
- 01-21-2011, 08:36 AM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
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
-
Using JSTL to capture HTTP referer data and serialize into a variable
By jeremy.wilson in forum JavaServer Pages (JSP) and JSTLReplies: 2Last Post: 05-19-2011, 12:44 PM -
Serialize array in java and unserialize in php
By chintan.vyas89 in forum New To JavaReplies: 2Last Post: 01-09-2011, 10:54 AM -
Serialize this!
By dustybray in forum Advanced JavaReplies: 1Last Post: 12-03-2010, 01:31 AM -
To serialize or not to serialize?
By XmisterIS in forum New To JavaReplies: 2Last Post: 09-21-2010, 01:37 PM -
serialize to web service?
By theartz in forum Advanced JavaReplies: 2Last Post: 08-16-2008, 01:39 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks