3

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 checkboxes 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.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Kurisu
  • 812
  • 10
  • 18

2 Answers2

2

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.

Tassos Bassoukos
  • 16,017
  • 2
  • 36
  • 40
1

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

Konstantin Komissarchik
  • 28,879
  • 6
  • 61
  • 61