Solved

Serialization of JTree an its Listeners

Posted on 2001-08-20
5
426 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Spring Framework HTTPSession management 1 45
factorial example 4 47
hibernate jars 4 45
Java Eclipse Loop 3 20
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 …
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

860 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