SWT:以编程方式设置单选按钮
当我创建几个单选按钮 (new Button(parent, SWT.RADIO)
) 并使用 radioButton5.setSelection(true)
以编程方式设置之前选择的单选按钮时也保持选中状态。我是否必须迭代同一组的所有其他单选按钮才能取消选择它们,或者是否有更简单的替代方案?提前致谢。
When I create a couple of radio buttons (new Button(parent, SWT.RADIO)
) and set the selection programmatically using radioButton5.setSelection(true)
the previously selected radio button also remains selected. Do I have to iterate over all other radio buttons of the same group to unselect them or is there a simpler alternative? Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不幸的是,您必须迭代所有选项。当您的 UI 第一次出现时,会触发
BN_CLICKED
事件。如果您的Shell
或Group
或任何单选按钮容器不是使用SWT.NO_RADIO_GROUP
选项创建的,则调用以下方法:所以本质上是 eclipse其本身取决于迭代所有单选按钮并切换它们的状态。
每次您手动选择单选按钮时,
BN_CLICKED
事件都会被触发,从而自动切换。当您使用
button.setSelection(boolean)
时,不会触发BN_CLICKED
事件。因此没有自动切换单选按钮。检查 org.eclipse.swt.widgets.Button 类以获取更多详细信息。
Unfortunately, you have to iterate over all the options. For the first time when your UI comes up then a
BN_CLICKED
event is fired. If yourShell
orGroup
or whatever container of radio buttons is not created withSWT.NO_RADIO_GROUP
option then the following method is called:So essentially eclipse itself depends on iterating over all the radio buttons and toggling their state.
Every time you manually select a Radio Button the
BN_CLICKED
event is fired and hence the auto toggling.When you use
button.setSelection(boolean)
then noBN_CLICKED
event is fired. Therefore no automatic toggling of radio buttons.Check the
org.eclipse.swt.widgets.Button
class for more details.同一组合中的单选按钮将充当一个组。一次只能选择一个单选按钮。这是一个工作示例:
The radio buttons within the same composite would act as a group. Only one radio button will be selected at a time. Here is a working example:
这应该会自动发生。您如何创建按钮?他们是同一个父母吗?父级是否使用 NO_RADIO_GROUP 风格?
This should happen automatically. How are you creating the buttons? Are they on the same parent? Is the parent using NO_RADIO_GROUP style?