Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

JSpinner serialization/deserialization  RSS feed

 
Marco Paolo
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my application, JPanels are written to files using ObjectOutputStreams. When a panel that includes a JSpinner is written to a file and then read back, the state of the spinner can no longer be changed. I've had no such problems with any other swing components.

The current value is ok, it loads correctly the last value i selected on the jspinner, but I just can't change value no more. Seems like the jspinner "forgot" it's model..


Any suggestions would be greatly appreciated, thanks.

 
Rob Spoor
Sheriff
Posts: 21089
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is your original SpinnerModel?

I've just checked the Java 1.6 source of JSpinner, and the model is transient. It will only be written if it is Serializable. If it isn't, it is discarded and the JSpinner will no longer have a model.

Now SpinnerNumberModel, SpinnerDateModel and SpinnerListModel are all Serializable so that shouldn't be a problem, but if you use a non-Serializable custom model that will be the cause.
 
Brian Cole
Author
Ranch Hand
Posts: 949
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Prime wrote:
I've just checked the Java 1.6 source of JSpinner, and the model is transient. It will only be written if it is Serializable. If it isn't, it is discarded


Aren't transient references discarded regardless if they are Serializable or not? (I had thought so, but I guess I've never tried it.)
 
Rob Spoor
Sheriff
Posts: 21089
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes they are, but the writeObject method serializes it through code, and the readObject deserializes it through code if present.
 
Marco Paolo
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I use SpinnerNumberModel, that is Serializable.
Maybe i have to declare it in a separate way?

Now I have only JSpinner as object-field and I have something like

SpinnerNumber model = new SpinnerNumberModel(....);
spinner = new JSpinner(model)

in the constructor. Isn't right? Anyway I also tried to declare the model like a field, but nothing.
 
Marco Paolo
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
any suggestion?
 
Rob Spoor
Sheriff
Posts: 21089
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you get when you print out the results of getModel()?
 
Marco Paolo
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't have the code here now, I will check tomorrow at work, so you are talking about print out to terminal the result of getModel() after the spinner is deserialized? I'll check it tomorrow, but please tell me, what are you thinking about?
 
Rob Spoor
Sheriff
Posts: 21089
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It should show us what the model is after serializing. Perhaps it is not what we think it is.
 
Marco Paolo
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Done, it says:

javax.swing.SpinnerNumberModel@bb5def
 
Marco Paolo
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have something like this

MAINCLASS:

JPanel mainpanel;
object1;
object2;
object3;

{ in the constructor of mainclass I inizialize the objects and add them to mainpanel. Then I have a singleton class where I create n objects from MAINCLASS and add MAINCLASS.mainpanel to the frame }

Nothing, it doesn't work. I realized that if I declare JSpinner in my class as transient, it still remember the last value, so it seems like the JPanel serialization saves the components "as they are". Don't know what to do.


help
 
Rob Spoor
Sheriff
Posts: 21089
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It saves the model just as we expected, so I have no idea what's going wrong.
 
Marco Paolo
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see. I tried in a different way, creating a "populate panel" method, and populating a new jpanel with serialized objects, but it's the same thing.

It seems like a default - bug or something like that, but I can't find solutions on the web. No one has ever noticed something like that or serialized a JSpinner with success?

 
Rob Spoor
Sheriff
Posts: 21089
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I never serialized any GUI component at all.
 
Marco Paolo
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Prime wrote:I never serialized any GUI component at all.


really? It surprise me a lot, as I thought that was a so common thing, as I see many programs implements persistence. So please tell me, you don't serialize gui component because you don't need to save application status, or you do that in another way?

In any case, I'm still in trouble with that problem, no one can help me?
 
Rob Spoor
Sheriff
Posts: 21089
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I need to store application status, I store just that - the status. So the value of a JSpinner or JTextField, etc. And I quite often use a Properties object for that.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!