File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes CheckBox state Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "CheckBox state" Watch "CheckBox state" New topic

CheckBox state

deepa sabbani

Joined: Apr 11, 2008
Posts: 8
i am new to swings. i have one doubt

we have one dialog box, in that dialog, there is one Check Box, Ok and Cancel buttons.
suppose user selected checkbox and click ok button, when user opens again that dialog box, the check box should be as selected.

can anyone give idea, how to implment this.

i know we can do this by using setSelected(true) and setSelected(false) methods.

but how to save the state of the check box.
Edwin Dalorzo
Ranch Hand

Joined: Dec 31, 2004
Posts: 961
Well, there could several ways to do that. One option could be not to dispose the dialog box, but to hide it. That way, next time you show it, the stated it had when it was hidden is still available. This could be a simple way, but it will not save the state after the application has been close.

If you need to dispose the dialog box, then you could save the state of the checkbox elsewhere, in another object, and then recover it when you create dialog box again.

Finally, if you need to save the state even after the application has been closed, then you will need to write this information to disk and recover it the next time the dialog box is painted.
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10825

Originally posted by deepa sabbani:

but how to save the state of the check box.

Easiest way to display a Dialog is use the appropriate JOptionPane.showXXX method.
These methods, among other parameters, accept "Object message".
Construct a JPanel, put your check box and other stuff you require inside that panel, and use the panel as the message Object. If you keep on referencing the same panel instance, the check box state will be always retained.

[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
I agree. Here's the link:
subject: CheckBox state
jQuery in Action, 3rd edition