• Post Reply Bookmark Topic Watch Topic
  • New Topic

Object serialisation & GUI

 
John Dell'Oso
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Howdy to all,

Not too sure if this is a Swing or an I/O serialisation issue. So I'll take my chances and post here.

I have 3 classes:

1) PostcodeStream - essentially writes and reads a PostcodePanel object.
2) PostcodePanel - sets up a list (JList) of cities and postcodes.
3) PostcodePanelTest - the application.

This application should allow you to select a city from a JList and it then displays the corresponding postcode in a JLabel (I know really exciting stuff ;-) ). I'm more interested in the object serialisation aspects of the application. The GUI has two buttons in the bottom panel - Save and Restore. The Save button writes the postcode panel object to file and the restore button restores the object back to the GUI.

Everything works fine (that is - I can save the object and then successfully restore it), except for the fact that when I click Save, the JList disappears altogether and just leaves the postcode label. What I expect is that the Jlist should not be removed from the GUI when the save button is clicked - but for some reason it does.

If you refer to the actionPerformed() method in class PostcodePanelTest for the btnSave JButton, all I do is call a method that writes the object to file. For the life of me I can't figure out why the JList "vanishes" from the GUI.

Here's the code for all three classes - the listings aren't too long I hope. I'd appreciate any feedback as to why I'm observing this behaviour for the save action. And again I apologise if this is not the right forum for my query.

Class - PostcodeStream.java
===========================



PostcodePanel.java
==================



PostcodePanelTest.java
======================



Thankyou and regards,

JD

[ July 21, 2005: Message edited by: John Dell'Oso ]
[ July 21, 2005: Message edited by: John Dell'Oso ]
 
John Dell'Oso
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry to be a nuisance - just a quick note inquiring as to whether anyone has any ideas regarding my query.

Cheers,
John D.
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John, as per our email this morning you mentioned wanting to test this technique to restore your view when the application is reloaded. This design has been discussed before, however, I can't recall if it is a valid/solid solution for what you are wanting to do. While you are waiting for replies, and since I can't figure out what the problem is myself, I'll research a bit on the actual usefulness of your idea. Obviously there are drawbacks or you wouldn't be posting a problem here. But I'd like to find out more.
 
Stuart Gray
Ranch Hand
Posts: 410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I also wondered why you would want to serialize the GUI itself. Personally I would just serialize the data and if you want to restore the state of the GUI, save the selected index as a preference value or similar.

Having said that, I also don't see why the code above shouldn't work. If you resize the frame does the list appear again?
 
John Dell'Oso
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stuart,

I don't see any reason why the code shouldn't work as well - that's why I have posted. I have considered saving the index, but this is just a simple test application for prototyping the concept for an application that is a little more "busy" than the test code. I just thought it would be handy to be able to save GUI component states and then be able to restore them when the application is run again without the need to store properties in a file.
Also, resizing the frame does not change the "vanishing" behaviour.

Anyway, the API documentation for JPanel (or any Swing component for that matter) contains the following paragraph:

Warning: Serialized objects of this class will not be compatible with future Swing releases. The current serialization support is appropriate for short term storage or RMI between applications running the same version of Swing. As of 1.4, support for long term storage of all JavaBeansTM has been added to the java.beans package. Please see XMLEncoder.



Without really understanding what "short term storage" really meant, I changed my PostcodeStream class to now use the XMLEncoder and XMLDecoder classes in place of the ObjectOutputStream and ObjectInputStream classes respectively. The code for this class now looks like so:



Now the good news is that the "disappearing JList" problem is resolved - it no longer vanishes when you save. However, the JList does not restore correctly, it always defaults to the first item in the list regardless of what it was when Save List was clicked. On checking the test.xml file that is used to store the objects, no details about the JPanel object are stored. The file contents are:



So it looks as though the use of the XMLEncoder and XMLDecoder classes has resolved the disappearing list issue - but now it doesn't restore the list correctly. The date object seems to be stored correctly but not the JPanel object. Well, that's my update on this problem. As usual, any suggestions on how to correct this are truly appreciated.

Cheers,
JD
[ July 28, 2005: Message edited by: John Dell'Oso ]
 
Tad Dicks
Ranch Hand
Posts: 264
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looking at your code...

you have a "writePostCode" method... any reason why this takes a JPanel? and not your "PostCodePanel" class ?

Your "readPostCode" panel returns a PostCodePanel, theoretically you could write a vanilla JPanel out and try to read it in as a PostCodePanel...

Lastly shouldn't PostCodePanel implement Serializable?

Why don't you just serialize the data, instead of trying to serialize the gui component. It just seems like a really convoluted method.

-Tad
[ July 28, 2005: Message edited by: Tad Dicks ]
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Tad Dicks:
Why don't you just serialize the data, instead of trying to serialize the gui component. It just seems like a really convoluted method.

-Tad

[ July 28, 2005: Message edited by: Tad Dicks ]


I believe John has already mentioned why he is going about this the way he is if you read the entire thread.
 
Stuart Gray
Ranch Hand
Posts: 410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just a quick link because I am at work: this newsletter talks about serializing GUI components over the network - I'm not sure if the concepts in it will be useful to you or not:

www.javaspecialists.co.za/archive/Issue013a.html
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!