当我创建几个单选按钮(new Button(parent, SWT.RADIO))的,然后将选择以编程方式使用radioButton5.setSelection(true)先前选择的单选按钮也保持选定。我必须遍历同组的其他单选按钮来取消选择它们或者是有一个更简单的方法吗?由于事先。

有帮助吗?

解决方案

不幸的是,你必须遍历所有选项。第一次当你的UI来了那么BN_CLICKED事件。如果您的ShellGroup或任何单选按钮的容器不与SWT.NO_RADIO_GROUP选项创建然后将下面的方法被称为:

void selectRadio () 
{
    Control [] children = parent._getChildren ();
    for (int i=0; i<children.length; i++) {
        Control child = children [i];
        if (this != child) child.setRadioSelection (false);
    }
    setSelection (true);
}

所以基本上蚀本身取决于在所有迭代的单选按钮和切换它们的状态。

每当你手动选择单选按钮BN_CLICKED事件被触发,从而自动切换。

当您使用button.setSelection(boolean)则没有BN_CLICKED事件。单选按钮的因此没有自动切换。

检查org.eclipse.swt.widgets.Button类的更多细节。

其他提示

相同的复合材料内的单选按钮将作为一个组。只有一个单选按钮,将在同一时间进行选择。下面是一个工作示例:

    Composite composite = new Composite(parent, SWT.NONE);

    Button btnCopy = new Button(composite, SWT.RADIO);
    btnCopy.setText("Copy Element");
    btnCopy.setSelection(false);

    Button btnMove = new Button(composite, SWT.RADIO);
    btnMove.setText("Move Element");

此应自动发生。你是如何创建按钮?它们是不是在同一个父?使用父NO_RADIO_GROUP风格?

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top