Solved

Serialization of JTree an its Listeners

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
How Complex Is This Java Course ? 9 63
countPairs challenge 7 58
mapAB Challlenge 35 89
maven example error 3 36
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

746 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

12 Experts available now in Live!

Get 1:1 Help Now