f



persisting state of components of a JFrame

Hi all...
I like to ask what is the recommended way of persisting the state of 
components of a JFrame... for example the size and location of a JFrame, 
the text in a text box, the isSelected property of a checkbox, the 
selection in a combo box and so on.

There are two approaches i can think of.

1) to save their values as strings, one by one, in a properties file, 
and then read them the next time the application is started, and apply 
them to the components.

2) serialize the whole JFrame and save it to a file, then read it the 
next time the application is started and use it instead of creating a 
JFrame using "new".


method 2 is obviously less work to do. but i am not sure if it is 
better, or if it will really work well.

does anyone have experience in this area who can shed the light?
also, is there yet another way of persisting this information that i 
have not thought about?

thanks for any help.
0
hilz
4/7/2006 5:46:12 PM
comp.lang.java.gui 8284 articles. 0 followers. Post Follow

2 Replies
472 Views

Similar Articles

[PageSpeed] 11

How about to create Serializable class that will contain anything you need?
In this case you won't need to serialize whole JFrame,
but only its state(s).

hilz wrote:
> Hi all...
> I like to ask what is the recommended way of persisting the state of 
> components of a JFrame... for example the size and location of a JFrame, 
> the text in a text box, the isSelected property of a checkbox, the 
> selection in a combo box and so on.
> 
> There are two approaches i can think of.
> 
> 1) to save their values as strings, one by one, in a properties file, 
> and then read them the next time the application is started, and apply 
> them to the components.
> 
> 2) serialize the whole JFrame and save it to a file, then read it the 
> next time the application is started and use it instead of creating a 
> JFrame using "new".
> 
> 
> method 2 is obviously less work to do. but i am not sure if it is 
> better, or if it will really work well.
> 
> does anyone have experience in this area who can shed the light?
> also, is there yet another way of persisting this information that i 
> have not thought about?
> 
> thanks for any help.
0
Vova
4/7/2006 5:54:17 PM
hilz wrote:

> I like to ask what is the recommended way of persisting the state of
> components of a JFrame... for example the size and location of a JFrame,
> the text in a text box, the isSelected property of a checkbox, the
> selection in a combo box and so on.
> 
> There are two approaches i can think of.
> 
> 1) to save their values as strings, one by one, in a properties file,
> and then read them the next time the application is started, and apply
> them to the components.
> 
> 2) serialize the whole JFrame and save it to a file, then read it the
> next time the application is started and use it instead of creating a
> JFrame using "new".
> 
> 
> method 2 is obviously less work to do. but i am not sure if it is
> better, or if it will really work well.
Sun discourages you from method 2. See the API doc of JFrame (or any other
Swing component):
<quote>
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.
</quote>

> 
> does anyone have experience in this area who can shed the light?
> also, is there yet another way of persisting this information that i
> have not thought about?
3) encode the whole JFrame via java.beans.XMLEncoder and save it to a file,
then read it next time the application is started via java.beans.XMLDecoder
and use it instead of creating a JFrame using "new".

-- 
"Thomas:Fritsch$ops.de".replace(':', '.').replace('$', '@')
0
Thomas
4/7/2006 6:00:14 PM
Reply: