Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free.

I am making program in Java in which I am making Dialog box, button, checkbox, etc. SWT widgets.

Now my problem is, I want like if in one execution button is made and in second execution checkbox is made then they both should display in one box. So I need to store that box object.

How can I do that?

share|improve this question
Have you looked into serialization? –  tdammers Nov 23 '12 at 11:54
Generally speaking, you can't serialize GUI objects. –  Stephen C Nov 23 '12 at 11:58
@Stephen C: Swing objects can be serialized, but it's not recommended. –  Michael Borgwardt Nov 23 '12 at 12:01
But SWT objects definitely can't. –  Stephen C Nov 23 '12 at 12:03
@StephenC: maybe not using standard serialization libraries (I'm not too familiar with the Java ecosystem, mind you), but saving the state of a GUI object into a byte sequence shouldn't be impossible. –  tdammers Nov 23 '12 at 15:27

2 Answers 2

In general, there are two ways to go about doing what you need:

  • Store the state of the object, and
  • Store the sequence of commands that lead the object to its current state

The first way is addressed by externalization of an object's state. Essentially, you write to an external storage of some sort a file with a description of the current state of the object:

Label at 40,10 text "Are you sure?"
Button at 40,20 size 30,10 text "OK"
Button at 40,40 size 30,10 text "Cancel"

You can read this file and restore the object to its current state.

The second way is addressed by storing commands, like this:

Add Label id 123 at 0,0
Move id 123 to 40,15
Set text id 123 "Are you crazy?"
Move id 123 to 40,10
Add Button id 124 at 0,0
Set text id 124 "Cancel"
Move id 124 to 40,40
Add Button id 125 at 0,0
Set text id 125 "OK"
Move id 125 to 40,20
Set text id 123 "Are you sure?"

When you have a file like that, you can "replay" it to get the current state of your object. But more importantly, you can use it to implement "undo" that survives exiting the application. If you add timestamps to the events, you would also get a way to restore the state as of some time in the past.

share|improve this answer

Basically you have to do this yourself. There are many ways to "persist" data and you're free to use any of them. You have to choose where the data will be stored first:

  • On the local computer
  • On a server on the local network
  • On a server somewhere on the internet?

...and you have to decide what format:

If you choose a Database, there are various frameworks like Hibernate that can help you.

So, when your application closes you have to hook that event and run some code that takes the program state you want to store and somehow gets it into your persistent storage. Then when the application starts up you have to open that storage, load the data, and rebuild the GUI the way you want it to be.

share|improve this answer
Java serialization won't work for SWT widgets. They don't implement Serializable. –  Stephen C Nov 23 '12 at 12:07
@StephenC - I'm not talking about serializing the SWT widgets. You can create a serializable class to be your "memento" where you store the data, and use that for easy writing/reading to/from a file. You still have to manually get the data you want to store, and do something with it when the application starts. –  Scott Whitlock Nov 23 '12 at 12:37

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.