• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Save and load the state of java GUI application

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI there,

I am new to GUI development, may be this is very easy to do, however, any help will be appreciated.

Considering that I have a GUI with 7-8 tabs, and each tab contains text boxes, check boxes and radio buttons, list-boxes and so on. The idea is to after filling some tabs, the user can save all the data (state of the components) in a file. And later on by loading the corresponding file, the user can retrieve all the data.

Here is an specific toy example.

Please consider, I have the following GUI application that collect student information:


As there are several tabs, the user may stop anytime ans save the information in a file, such as:


The idea is that the user can load the file later and continue his/her task.

I would like to know how I can save the state of all the components in the application and load later on.

Thanks in advance.
 
Ranch Hand
Posts: 54
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try serialization. If you don't know what that is, do some googling around.
 
shopno dekhi
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

William Ng wrote:Try serialization. If you don't know what that is, do some googling around.



I have heard serializing GUI component is not a good idea.  
 
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can use a Properties files to save/load the data.

You will still need to recreate the GUI and set each component to its respective property.
 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can build XML or other type of files
for example functions for save and load the same files

 
Saloon Keeper
Posts: 15490
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not a big fan of writing all this procedural code to deal with XML files. Code becomes spaghetti, hard to maintain and debug. Instead, if you want to save your application state as XML, I think using something like JAXB works much better.

You should have separate presentation, logic and persistence layers. Let's say the logic model looks like this:

You can then add the following class to your persistence layer:

If all other types are implemented in a similar way, JAXB.marshal() will yield an XML like this:
 
shopno dekhi
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Camick wrote:You can use a Properties files to save/load the data.

You will still need to recreate the GUI and set each component to its respective property.



Thanks. Can you provide me a little example.
 
shopno dekhi
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Stephan van Hulst,  I agree to use JAXB for XML files. What I do not understand is that if I have a big complicated GUI (which has multiple Textbox, list, combo box; some of them are already selected) how can I save the state of all this component. DO I need to iterate all the components?

Thanks.
 
Stephan van Hulst
Saloon Keeper
Posts: 15490
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No. You should add listeners to your components that update your business model as soon as the components change. That way your model always stays in sync with the user interface. Then, when you click 'save', you can just convert the business model to a JAXB model, and marshal it. For the other way around, you just add an update() or refresh() method to your user interface that reads the data from your business model and updates all your components.
 
shopno dekhi
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks. As I am very new, Can you please explain what do you mean by a business model. Moreover, how could I able to load the states of components from xml file.
 
Stephan van Hulst
Saloon Keeper
Posts: 15490
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The business model is a model of your problem domain, without concerns such as GUIs, persistence, networking, etc. Take a look at the code I posted earlier. Classes like Person, Course and Curriculum are part of the business model. They are the things you want to think about and interact with. The GUI classes are just tools for displaying the business model. The data classes are just tools for storing the business model.

You don't load your GUI components from XML. Your application controllers load the XML as data models, convert the data models to business models, and then tell the GUI to display the business model.

Are you familiar with the MVC (model-view-controller) paradigm? If not, you should read up on it and understand what it means.
 
shopno dekhi
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, @Stephan van Hulst. I was not familiar with MVC architecture. After some reading, I have got the main concept of the architecture. However, there are some questions remains.

You don't load your GUI components from XML. Your application controllers load the XML as data models, convert the data models to business models, and then tell the GUI to display the business model.



I understand that I do not need to load the GUI from the xml file. However, I need some method or function to the GUI (view) to iterate over all the components the view has. And the controller retrieves the data from the Model and sent it to the view to display. So, my question is that do I need to iterate over all the components as I have around 100 component with different names.

 
Stephan van Hulst
Saloon Keeper
Posts: 15490
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't know why you have to iterate over your components in the first place. You reference the controls that hold data directly. In the screenshots you posted, those are 5 tops: Three controls to describe the students, and one listbox per year to select courses.
 
shopno dekhi
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote: In the screenshots you posted, those are 5 tops: Three controls to describe the students, and one listbox per year to select courses.



Sorry, this is just a toy example that I made to show the problem. In my original problem, I have approximately 100 GUI components.
 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But those 100 GUI elements must represent something.
And that "something" is your model.
 
shopno dekhi
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dave Tolls wrote:But those 100 GUI elements must represent something.
And that "something" is your model.



Thanks. I know this is model, my question is that is there any efficient way to load the data from model to view where a view has many components.  
 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I will ignore that you said "efficient" because that's a meaningless term here. But it shouldn't be difficult if the GUI and the model are set up properly. So when you read the model's state from the external location you would call methods in the model which set the appropriate data items. And just as in normal operation, setting those methods in the model would cause the listeners (the GUI) to be notified of changes, and hence the GUI components would also be initialized with that saved data.
 
shopno dekhi
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:And just as in normal operation, setting those methods in the model would cause the listeners (the GUI) to be notified of changes



As I am new, I do not understand how to do that. Can you please have a small example. That would help a lot.

Thanks.
 
reply
    Bookmark Topic Watch Topic
  • New Topic