• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 439
  • Last Modified:

Serialization of JTree an its Listeners

I have problem to serialize a JTree and its listeners.
The tree is serialized/deserialized well but my listeners disapears.

explorerTree = (JTree)Serializer.deserialize("dragtree7");
if (explorerTree == null)
  //create the explorer tree and add the listeners
The lis_1 echos "lis_1". This works fine in the application, but If I close it, start it and deserialize my tree then the listener is gone(no text echos when I selects a node).

But if I add another listener to the tree after deserialization.
lis_2 listener echos "lis_2".

And if I now select a node in the tree I got TWICE echos "lis_2". What happens? My first listener seems to be there but action code is replaced.

If I close application and start it again by adding a new listener, I got three echos "lis_2" and so on.

1 Solution
lawpanAuthor Commented:
Please, I cant figure it out why my listeners goes.

A few comments that might help:
1) serializing a JTree is a bad idea, you should never serialize a Swing component, you should serialize the Data model instead. In this case, a JTree's model is transient which means that there is no guarantee that the tree will unserialize with any model at all.
2) Serializing a component with a listener list can also be a bad idea. When you add a listener to an object you are really pointing to it in the VM. if you serialize and deserialize during the same runtime session it  *might* work, but if you deserialize on another client or in another runtime session, there is no guarantee( nor is it even likely) that those listener objects will exist.  A listener is just a pointer (to use a C++ comparison) to an object of the right type, and if you save that pointer, there is no guarantee that it will point to the right object on another client or even during another session. What you should do is:
get the model from the JTree
Remove all the listeners from the model
Serialize the model.

Deserialize the model
re-add listeners
set the tree's model.

Related text:
Exerpt from JTree source:
     * The model that defines the tree displayed by this object.
    transient protected TreeModel        treeModel;

     * Models the set of selected nodes in this tree.
    transient protected TreeSelectionModel selectionModel;

Hope this helps,
Igor BazarnyCommented:

Kylar is  right, better way is serializing your data model and registering listeners separately.

Serialization isn't that bad as kylar describes. After deserialization you will get valid object references no matter where you deserialize (at least if .class files for serialized objects are same), but listemner serializing is still bad idea because deserialization will create another copies of objects, so you will probably get multiple copies of unexpected objects (e.g. copies will be created for objects referenced by listeners, explicitely or implicitely via references from inner class to enclosing context). You don't want to serialize whole application, do you?

Igor Bazarny,
Brainbench MVP for Java 1
JTree's listenerer are transient attributes for the reason's given above and as such are excluded from the serialization purpose.
lawpanAuthor Commented:
Hmm about your point 2)
Java dont uses pointers. Its uses references.
A listeners is an object that will also be serialized because JTree holds a reference to it.

But as "objects" says that listeners have transient attributes that will not be serialized.

But ok I accept the answer. Thanx you all for advices. I undetstand serializeing, but would like to read more about serializing swingcomponents. Do any of you have some URLS describing that?

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now