persisting state of components of a JFrame

  • Permalink
  • submit to reddit
  • Email
  • Follow


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
Reply hilz 4/7/2006 5:46:12 PM

See related articles to this posting

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
Reply 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
Reply Thomas 4/7/2006 6:00:14 PM
comp.lang.java.gui 8292 articles. 14 followers. Post

2 Replies
226 Views

Similar Articles

[PageSpeed] 49

  • Permalink
  • submit to reddit
  • Email
  • Follow


Reply:

Similar Artilces:

Persistent vegetative state
As a gamer, I consider myself in a persistent vegetative state. Please, somebody kill me. p.s. cut off my supply of twinkies On 26 Mar 2005 16:09:22 -0800, "zZero" <jsk2@excite.com> wrote: >As a gamer, I consider myself in a persistent vegetative state. Please, >somebody kill me. Your status as a vegetable qualifies you for indefinite protection by the Shrub Patrol. Death To The Salad Eaters! "zZero" <jsk2@excite.com> wrote in message news:1111882162.915682.7010@g14g2000cwa.googlegroups.com... Please, somebody kill me. Post your address. OK, ...

Using components in state machines
Hello all - I have a question regarding said topic in the subject line. I am trying to use a previously created VHDL module (an 8-bit divider) in a larger state machine architecture and I'm having some problems with syntax. I have declared the module as a component as such: component div8 port( diva,divb : in std_logic_vector(7 downto 0); div0 : out std_logic_vector(7 downto 0); divq : out std_logic_vector(7 downto 0)); end component; The state machine is based around a case statement that jumps to the state that calls the divider whenever a control value is set to "%10"....

Changing JFrame components at runtime
In my JFrame I have a JPanel's extension class with a 2-dimensional array of 3X3 JLabels. This array is, of course, created in initComponents() of MyPanel class. I've put a menu item in which user can change the dimension of JLabels' grid. If, e.g., the user chooses 4 then MyPanel should change that grid to a 4x4 array of JLabels. How can I do that? Can I invoke again initComponents() methods of MyPanel and JFrame? Thanks. lego_lax wrote: > In my JFrame I have a JPanel's extension class with a 2-dimensional > array of 3X3 JLabels. > This array is, of course, created...

JFrame is displayed before all components are added
In my project all JFrames are undecorated (setUndecorated(true)) and many graphical components are added to the JPanel. But, before all components and colors are added to a new JFrame(), the user can see for 1/2 seconds the Frame as gray,undecorated. How can it be fixed? I set the (J)Frame visible (setVisible(true)) after all components are added to the JPanel of the Frame. Theodosis Theodosis Ekizoglos wrote: > In my project all JFrames are undecorated (setUndecorated(true)) and > many graphical components are added to the JPanel. > > But, before all components and colors ar...

Problem in displaying components in JFrame
I created a class extending JFrame I created a frame containing several components After making it Visible by setVisible(), I have used a timer. When the timer expires the function returns, But unless and until the function returns,the components on the frame are not being displayed.. But I want them to be displayed during the timers time span. Please Help! On Mar 19, 6:47 am, snehapshi...@gmail.com wrote: > I created a class extending JFrame > I created a frame containing several components > After making it Visible by setVisible(), I have used a timer. > When the timer expires t...

adding a component to an existing JFrame
I want to add a component to an existing JFrame when the user selects a certain menu item. I have succeeded in creating the menu and catching the event in an actionPerformed method, but I can't get the new component to appear. I have tried: JButton button = new JButton(); add(button, BorderLayout.CENTER); repaint(); I also tried creating the component ahead of time, making it invisible, and then setting visibility to true in response to a click on the menu item. Neither approach worked. Your advice is welcome (including advice to post this message in a different group). Thanks What a...

Beginner Question on State Machine and Components
Help! I have a state machine on my top level for an SRAM controller. The states are sram_state_read, sram_state_write, sram_state_idle, and sram_state_reset. Presently the design is flat with no components. I want to put the SRAM paths in a component by itself. Since the SRAM state machine may influence other parts of the design, I want to have a separate component for it, or leave it in the top level. Now it seems to me that one should be able to pass these states down to to the SRAM paths' component using the type that I decleare on the top level. However I have been unable to find th...

Swing Components not staying where they are dragged to on the JFrame
I've been trying to figure this out for the last few weeks, so if you could help me with the following conundrum, I would very much appreciate it: I have 2 jpanels and a jbutton. I add multiple jlabels on one of the jpanels by pressing the jbutton, which is located on the jpanel opposite to the one where the jlabels are added ("destination" jpanel). I have also implemented drag-and-drop code, so that, at any point in time, the added jlabels can be dragged to a different location on the destination jpanel. The problem that I am facing is that the added jlabels go back to t...

Saving/loading ui component state
Hi The UI swing components used by my application uses a UIModels object that stores the individual ui swing component models and is responsible for saving and loading the ui components data. The panel containing the ui swing components receives a reference to the UIModels model to get at each individual ui swing component model, as shown below: class UIModels { private ButtonModel buttonModel_1; private ButtonModel buttonModel_2; public ButtonModel getButtonModel_1() { return buttonModel_1; } public ButtonModel getButtonModel_2() { return buttonModel_2; } public voi...

Rails
I am trying set up a rails app that utilizes components. I need to be able to pass some state (a bunch of name value pairs) from the Main controller to the component. Is there any way to pass this state info using render_component? Regards Check the docs, but I believe you can pass a hash called :params with any name-value pairs you need. They then show up in the @params instance variable in the components controller: render_component :controller => "foo", :action => "bar", :params => { :baz => "bil" } .... class FooController .... def bar ...

Setting enabled state of all text components ?
Hi, How would I go about programatically finding all the JTextFields in a Frame (in all the contained panels and so on) so that I can set the enabled state. This would be usefull as it means that I don't need to keep adding extra enabled code when other people add extra fields to a derived gui class. Cheers Steve Steve Webb wrote: > Hi, > > How would I go about programatically finding all the JTextFields in a > Frame (in all the contained panels and so on) Call getComponents() on the frame. For each component in the list that is an instanceof Container (all of them, in ...

Execution state persistence for workflow application
Hi all, I'm pretty new to the python language so please excuse me if this is FAQ... I'm very glad to be part of the list! :-) I'm looking into a way to implement a generic workflow framework with python. The basic idea is to use python scripts as the way to specify workflow behavior. The framework should not only use scripts as a specification language but is going to leverage on python interpreter for the execution of the scripts. One of the possible solution is to have a one to one mapping between a interpreter process and a workflow instance. Since I want to be able to model ...

Display State- components changing randomly!
Has anyone seen stuff change display modes suddenly in inactive display states? I've got 10 display states, each with most items in Hidden Lines Removed, but a few relevant components in shaded mode. Switching between display states lets you see different sets of things in full Shaded with Edges modes while the others are sort of wireframe (but HLR = better)- SWEET! However, sometimes, switching to a different display state shows that a whole bunch of items are back to Shaded with Edges! At first I thought it was related to right clicking/changing display mode there vs. using th...

Problem with persistence of window state between sessions
Hello, Probably, since I updated Netbeans or JDK (but not sure it is a reason of problems), there is a problem with persistence of window state between sessions. Before, when I changed something (eg. table's size of column, SplitPane splitter position), closed the application and then started it again, all settings were the same as I set. Now, when I close my application, no settings from main form are stored -- when I run app again, it looks "default". On my app's menu, there is "about box" and one simple dialog with table -- state of these two is stored and resto...

POE::Component::IRC::State and nick changes
Using POE::Component::IRC::State, I'm trying to access channels in which my Perl program sees a user has changed his nick. This is the relevant subroutine: sub onnick{ my ($who,$newnick,$where) = @_[ARG0,ARG1,ARG2]; my ($nick, $host) = split /!/, $who; my $channel = $where->[0]; my $logline = "--- " . $nick . " is now known as " . $target; &writeline($ircserver, $channel, $logline); } Now, the documentation indicates that ARG2 will contain an arrayref of all the channels on which the client saw the user change his ...

POE::Component::IRC::State and nick changes
Using POE::Component::IRC::State, I'm trying to access channels in which my Perl program sees a user has changed his nick. �This is the relevant subroutine: sub onnick{ � � � � my ($who,$newnick,$where) = @_[ARG0,ARG1,ARG2]; � � � � my ($nick, $host) = split /!/, $who; � � � � my $channel = $where->[0]; � � � � my $logline = "--- � � " . $nick . " is now known as " . $target; � � � � &writeline($ircserver, $channel, $logline); } Now, the documentation indicates that ARG2 will contain an arrayref of all the channels on which the client saw the user change his ...

Component in rollback state; assembly is inaccessible now.
Really weird... The assembly has a mirrored sub-assembly. I then opened the original, un-mirrored subassembly and added some parts. SW wouldn't let me back into the main assembly, complaining about a component in rollback state. Couldn't close the main assembly, and couldn't exit SW. I had to kill it from Task Manager. Any idea what's going on? It's one thing to not update the mirrored subassembly, but it shouldn't invalidate the main assembly. It's fine after restarting SW, but missing the new parts on the mirrored side. It sure would be nice if SW would tr...

Designing a filter with changeable frequencies and persistent states
Hi. I am working on an application where we want to low-pass filter a signal quite heavilly, but we want to change the cut-off frequency now and then. Is this possible? I have previously solved this with simply creating a new fitler when the cutoff needs to be changed, but the problem then is that the state is reset and the filter begins at a value of 0 after each reset. This in turn causes a large discontinuity in my signal every time the filter is changed. I was hoping to avoid this discontinuity. I have assumed that simply saving the state of one filter and then applying it to anothe...

[ciao-users] saving and loading the state of a component
------=_Part_15428_14159944.1184247966510 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline CIAO VERSION: 0.5.8 TAO VERSION : 1.5.8 ACE VERSION : 5.5.8 HOST MACHINE and OPERATING SYSTEM: i686 pc, SUSE linux Enterprise Server 9, Kernel 2.6.5 COMPILER NAME AND VERSION (AND PATCHLEVEL): gcc 3.3.3 (SuSE Linux) THE $ACE_ROOT/ace/config.h FILE: #include "config-linux.h" THE $ACE_ROOT/include/makeinclude/platform_macros.GNU FILE : include $ACE_ROOT/include/makeinclude/platform-linux...

Modelling persistent data with stateful session beans
Hi, we are having discussion in our development team about choosing the right bean type for our business data. The data in question is a list of open orders for a product. There are two proposals on the table: 1) Stateful session bean (the state representing the data, ie. the product number and the list of orders for the product) 2) Entity bean (containing the product number and the order list, ie. coarse-grained) We assume in both cases the bean would handle its persistence itself (BMP for the entity bean). For the entity bean we assume it would NOT be clusterable (because w...

Russian invent persistent state objects OS ..
In Phantom, powering off the computer will not cause programs to lose state. They can pick up where they left off as soon as the machine is turned back on .. Phantom doesn't have files .. a file in Phantom is simply an object whose state is persisted. You don't have to explicitly open it. As long as your program has some kind of reference to that object, all you need to do is call methods on it, and the data is there as you would expect .. http://www.theregister.co.uk/2009/02/03/phantom_russian_os/ Doug Mentohl wrote: > In Phantom, powering off the computer will not cause pr...

svc manifest import problem and state persistence problem
I have a svc manifest I wrote (cribbed from Sun template). It's very simple. I'll attach it at the end. First problem is that when I install the software and the manifest, I run svccfg import. However on first reboot, it gets reimported. On subsequent reboots it's fine. Why wasn't my import at install-time sufficient? Second problem is that the service doesn't stay enabled across reboots. I'm using 'svcadm enable dns/named' to start it. (NOTE: no -t option). I have another service which does stay enabled. I don't see the difference between the manife...

Manually added routes persisting through interface state changes
We ran into a weird problem tonight where a power failure took down much of our network including a switch that 2 Linux servers connect to. These 2 servers are connected to 2 different networks on 2 Ethernet nics. The routing table has the default pointed out eth0 (the interface that leads to a public IP via NAT on a Pix), a connected route for eth0, a connected route for eth1 and finally a static route also pointed out eth1. The problem as we've determined it is that the static route was dropped from the route table when eth1 went down. I haven't had time to confirm this in a lab ...

Web Framework with stateful sessions / persistent object space?
Not sure if the subject is fitting, but I couldn't think of anything else. I am looking for a web framework in Perl that lets me cache global objects that I can access from anywhere within the perl code that is being called upon a new request. Basically I want to use this to set up central object stores, Database connection caches, etc. The framework should also support session management. Can anybody recommend something like this? ...