Solved

Serialization of JTree an its Listeners

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
groupSum5 challenge 5 71
get weblogic logged in user in java 2 58
mockito example issue 8 64
HSSFWorkbook cannot be resolved error 10 50
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

867 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now