Restrict how many checkboxes can be selected

Hi,How can i go about designing a group of four checkboxes, but ensure the user can only select 2 out of 4.I know i can create a CheckboxGroup and add the 4 checkboxes in, but that would mean all are mutually exclusive.Any ideas?
[257 byte] By [iPortala] at [2007-11-27 2:29:25]
# 1
add a listener to your 4 checkboxes. Create a method to count the number of boxes that are selected. If that number is greater than 1, call setEnabled(false) on your other boxes, otherwise setEnabled(true)
tjacobs01a at 2007-7-12 2:42:34 > top of Java-index,Java Essentials,New To Java...
# 2
Thanks, i'll give that a go.
iPortala at 2007-7-12 2:42:34 > top of Java-index,Java Essentials,New To Java...
# 3
Would this allow me to check this at run time, or when a submit event occurs (for example)?
iPortala at 2007-7-12 2:42:34 > top of Java-index,Java Essentials,New To Java...
# 4
I would extend the ButtonGroup class to make it allow any arbitrary number of selections instead of 1. It might be easier to implement something like that from scratch though insead of using ButtonGroup, I don't know.
hunter9000a at 2007-7-12 2:42:34 > top of Java-index,Java Essentials,New To Java...
# 5
> Would this allow me to check this at run time, or when a submit event occurs (for example)?Err, is this a Swing app or a web app?
DrLaszloJamfa at 2007-7-12 2:42:34 > top of Java-index,Java Essentials,New To Java...
# 6
Swing app.It's a tutorial. So when someone selects two checkboxes from a possible 4, i would ideally like to tell them before submitting the answers.Regards,
iPortala at 2007-7-12 2:42:34 > top of Java-index,Java Essentials,New To Java...
# 7

> Swing app.

>

> It's a tutorial. So when someone selects two

> checkboxes from a possible 4, i would ideally like to

> tell them before submitting the answers.

>

> Regards,

In that case, you could just check when the user submits the answers. If 2 of the buttons are checked, then go ahead, otherwise, do something else.

hunter9000a at 2007-7-12 2:42:34 > top of Java-index,Java Essentials,New To Java...
# 8

> I would extend the ButtonGroup class to make it allow

> any arbitrary number of selections instead of 1. It

I thought of this but it might be tricky: if you have 2 selections checked and someone clicks on a third, which of the 2 gets turned off? I'm sure you could solve this with a LIFO or FIFO policy, but IMO I think disabling the other checkboxes is a cleaner solution.

tjacobs01a at 2007-7-12 2:42:34 > top of Java-index,Java Essentials,New To Java...
# 9

> > I would extend the ButtonGroup class to make it

> allow

> > any arbitrary number of selections instead of 1.

> It

>

> I thought of this but it might be tricky: if you have

> 2 selections checked and someone clicks on a third,

> which of the 2 gets turned off? I'm sure you could

> solve this with a LIFO or FIFO policy, but IMO I

> think disabling the other checkboxes is a cleaner

> solution.

You wouldn't have to uncheck one of them when the third was checked. You could just disallow the third selection, either by unchecking it when there are already two selections, or by disabling the other boxes when there are two selections. From a user perspective, I think the second option is better, since it gives visual feedback as to why the third box can't be selected.

hunter9000a at 2007-7-12 2:42:34 > top of Java-index,Java Essentials,New To Java...