如何创建允许取消选择实际选项的 JToggleButton 的 ButtonGroup?
就是这样。我需要创建一个 ButtonGroup,允许选择一个选项,或者,如果用户单击所选选项,则取消选择该项目(不会选择任何内容),当然,还要捕获事件来执行某些操作。
That's it. I need to create a ButtonGroup that allows to select a option or, if the user click on the selected option, deselect the item (nothing will be selected) and, of course, capture the event to do something.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
以防万一杰夫的链接将来被破坏,描述如下:
您需要将 ButtonGroup 子类化以允许不选择,并将按钮添加到此按钮组中。
Just in case Jeff's link is broken in the future, here's what's described:
you need to subclass ButtonGroup to allow a no-selection, and add your buttons to this buttongroup.
这准确地展示了如何做到这一点 https://dzone.com/articles/unselect-all -切换按钮
This shows exactly how to do that https://dzone.com/articles/unselect-all-toggle-buttons
在未选中的按钮/复选框上执行
button.setSelected(false)
时,我注意到奇怪的行为。它取消了所有内容,就像我取消了某些内容一样。我是这样修复的:
I noticed weird behavior when doing
button.setSelected(false)
on a button/checkbox that is not selected. It deselected everything as if I deselected something.I fixed it this way:
捕捉事件来做某事。还要执行以下操作。
编辑:
但没有涉及 ButtonGroup。
Capture the event to do something. Also do the below.
EDIT:
But there is no ButtonGroup involved.
java 1.6之前的解决方案
Solution for pre java 1.6