Serialization and JComponents with ActionListeners
Hi, I got a quick question:
Is there a chance to send JComponents which come with an ActionListener over the net for example via RMI?
I always get the following Exception:
java.lang.ClassCastException: server.ActionTryImpl_Stub cannot be cast to java.util.EventListener
java.lang.ClassCastException: server.ActionTryImpl_Stub cannot be cast to java.util.EventListener
at javax.swing.event.EventListenerList.readObject(EventListenerList.java:255)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at java.io.ObjectStreamClass.invokeReadObject(ObjectStreamClass.java:974)
at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1846)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1753)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1329)
at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:1945)
at java.io.ObjectInputStream.defaultReadObject(ObjectInputStream.java:480)
at javax.swing.JComponent.readObject(JComponent.java:5382)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at java.io.ObjectStreamClass.invokeReadObject(ObjectStreamClass.java:974)
at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1846)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1753)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1329)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:351)
at sun.rmi.server.UnicastRef.unmarshalValue(UnicastRef.java:306)
at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:155)
at server.ActionTryImpl_Stub.getJComponent(ActionTryImpl_Stub.java:56)
at client.TransferClient.main(TransferClient.java:33)
Please dont ask the sense of doing that, I just have to send them via RMI!
Is the only solution to analyse the JComponent at one side and put the information in an XML Document , then send this document via rmi over the net and rebuild the JComponent with Listeners on the other side?
Thanks for awnsering these questions!
[2650 byte] By [
Sandmann6a] at [2007-11-27 10:29:22]

# 2
Of couse I can tell you the main problem I have.
OK:
I have a system on one side that on exists of an empty JFame.
The other sides are different components that all have their own GUI-elements with implemented listeners.
An nice example for something like zhat in reality would be:
You have an remote control with an initially empty screen. Now when you turn for example your DVD-Player on, you get the GUI that comes with your DVD player on the screen of your remote control. When you turn your TV on, you get the GUI element of the TV on your remote control. This should happen via RMI in my example.
I hope my problem gets a bit clearer now. Of course the GUI elements have (for example an JPanel with several JButtons) several Listeners and I want to react to the JButtons if they are pressed on my Remote Control.
The main problem I have know is to serialize for example an ActionListener. The JComponents are no problem, they can be send via RMI.......but the Listeners.
Its for a thesis on my university. And i need an answer pretty quick, because time is running out.
The main thing I have to know: THE WHY? Why are they not serializable? I have a very easy example: I have a class which implements ActionListener. If I want to send this ActionListener via RMI, I get the Exception shown above.
Thanks much for your help
# 3
Ah, if I get you right, you want to basically remotely configure a JFrame with listeners. Ok, you've got a couple of options here. I
f you have a known set of Listeners, that is fixed, you have those classes at both ends, and part of the configuration for the component will include a list of strings which are the classnames of those listeners. You can easily serialize that and send it across the wire. At the other end, your component used a bit of reflection to instantiate the appropriate listeners and add them to itself.
If you want to add new types of listener without having to have them available on the other side of the wire up-front, you could have a custom classloader which pulls its class bytes over the wire, loads the class(es) then instantiates and adds them
The reason ActionListeners aren't serializable is that it doesn't really make sense to serialize them. They don't (typically) maintain state, so there's nothing other than the class bytes to be serialized, and class bytes are already serialized since they exist as, well, a stream of bytes
If that's not clear, do ask me to explain. I'm just in a bit of a rush at the moment!
# 7
Ok, lets look at a concrete example.
The Service Interface is something like that:
public interface ButtonTest extends Remote{
public JComponent getJComponent() throws RemoteException;
}
And then I have a class
public class Test implements ButtonTest, ActionListener{
public JComponent getJcomponent(){
JButton button = new JButton();
button.setText("TEST");
button.addActionListener(this);
return button;
}
public void actiuonperformed(Event e){
System.out.println("THIS IS A TEST");
}
}
Now I want this button on the other side with the same behaviour. Is there a direct way which makes this possible ? Im just geeting crazy because of that problem!
Message was edited by:
Sandmann6