DanielLC
DanielLC

Reputation: 7151

In Java Swing, is there a way to toggle ButtonGroups being mutually exclusive?

From what I understand, if you want to have to select one option, you put it in a ButtonGroup, and if you don't, you don't use a ButtonGroup. But I want it so that multiple can be selected but only if you selected something else in another area. Is there anything I can do besides remove each element from the button group one at a time?

Upvotes: 0

Views: 96

Answers (1)

camickr
camickr

Reputation: 324098

Is there anything I can do besides remove each element from the button group one at a time?

  1. Manually removing buttons seems simple enough to do.

  2. Or you could extend ButtonGroup and create a removeAllButtons() method. By extending ButtonGroup. You will have access to the Vector that holds all the buttons in the group. Then you just invoke the removeAllElement()method of the Vector.

  3. You could create a custom ButtonModel. See: https://stackoverflow.com/a/44183140/131872 as an example of this approach. You could modify the logic such that a size value of "-1" indicates there is no selection limit.

Upvotes: 0

Related Questions