开发者

Creating a Button Group Allowing N checked boxes

开发者 https://www.devze.com 2023-03-23 09:43 出处:网络
I am programming in Java, using Swing. I am currently working with an application which allows the user to display 2 or less hobbies.The list of hobbies is finite.I would like to provide a user with

I am programming in Java, using Swing.

I am currently working with an application which allows the user to display 2 or less hobbies. The list of hobbies is finite. I would like to provide a user with a list of checkb开发者_运维百科oxes to select these items from, allowing them to check up to 2 options, but no more.

What are my options for implementing this? Is there a ButtonGroup like object that can hold these items?

I'm trying to avoid having 2 Combo boxes for this, as if the arbitrary 2 limit is increased (to size n) It'd be a pain to scale.


Hook up the action event of each checkbox and count how many are checked every time this handler is invoked. If as many are checked as it is allowed, disable every unchecked checkbox, else enable it.

Otherwise, just disable the submittion button and add a label explaining the situation to the user - and re-enable it as soon as the active number of checkboxes drops below the threshhold.


Consider using checkbox list (list control with checkbox next to each item). This approach scales better than producing a separate checkbox per item.

0

精彩评论

暂无评论...
验证码 换一张
取 消