Solved

Serialization of JTree an its Listeners

Posted on 2001-08-20
5
421 Views
Last Modified: 2012-06-27
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
  explorerTree.addTreeSelectionListener(lis_1)
}
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.
  explorerTree.addTreeSelectionListener(lis_2)
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.

/Lawpan
0
Comment
Question by:lawpan
5 Comments
 

Author Comment

by:lawpan
ID: 6406570
Please, I cant figure it out why my listeners goes.

/Adrian
0
 
LVL 4

Accepted Solution

by:
kylar earned 200 total points
ID: 6407514
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.

THEN
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,
Kylar
0
 
LVL 7

Expert Comment

by:Igor Bazarny
ID: 6407591
Hi,

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?

Regards,
Igor Bazarny,
Brainbench MVP for Java 1
www.brainbench.com
0
 
LVL 92

Expert Comment

by:objects
ID: 6408008
JTree's listenerer are transient attributes for the reason's given above and as such are excluded from the serialization purpose.
0
 

Author Comment

by:lawpan
ID: 6408714
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?
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Java Server Faces parameter pass? 6 50
eclipse shortcuts 9 55
java jdbc batch example 8 35
eclipse buid path vs tomcat lib path 10 23
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question