Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/343.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 取消选择组的所有SWT单选按钮_Java_Eclipse_Radio Button_Swt_Radio Group - Fatal编程技术网

Java 取消选择组的所有SWT单选按钮

Java 取消选择组的所有SWT单选按钮,java,eclipse,radio-button,swt,radio-group,Java,Eclipse,Radio Button,Swt,Radio Group,我有两个SWT工具项,在工具栏中有样式SWT.RADIO。我想使用tooltItem.setSelection(false)以编程方式取消选择它们,但这是不可能的。当前选择的工具项保持选中状态。我可以使用ToolItem.setSelection(true)选择另一个ToolItem,这将取消选择第一个,但是我找不到一种方法来取消选择所有的ToolItem,尽管当GUI启动时,所有的ToolItem都被取消选择。有人知道如何取消选择所有这些选项吗 更新:我找到了问题所在:我检查了@rgeorge

我有两个SWT
工具项
,在工具栏中有样式
SWT.RADIO
。我想使用
tooltItem.setSelection(false)
以编程方式取消选择它们,但这是不可能的。当前选择的
工具项保持选中状态。我可以使用
ToolItem.setSelection(true)
选择另一个ToolItem,这将取消选择第一个,但是我找不到一种方法来取消选择所有的ToolItem,尽管当GUI启动时,所有的ToolItem都被取消选择。有人知道如何取消选择所有这些选项吗

更新:我找到了问题所在:我检查了@rgeorge,发现
tooltItem.setSelection(false)
有效,如果工具栏是用
toolbar toolbar=new toolbar(shell)
构建的。但是,如果我使用Mac(窗口框架统一工具栏)上的
shell.getToolBar()
可以获得的工具栏,它将无法工作。似乎是SWT不兼容。下面是一些代码来重现效果(更改
useShellToolbar
以在Mac上的案例之间切换):

//取自SWT代码段47
包org.eclipse.swt.snippets;
导入org.eclipse.swt.*;
导入org.eclipse.swt.graphics.*;
导入org.eclipse.swt.widgets.*;
公共类工具栏RadioButtonGroupTest{
公共静态void main(字符串[]args){
布尔useShellToolBar=true;
显示=新显示();
外壳=新外壳(显示);
图像=新图像(显示器,20,20);
Color Color=display.getSystemColor(SWT.Color\u BLUE);
GC=新的GC(图像);
gc.setBackground(颜色);
gc.fillRectangle(image.getBounds());
gc.dispose();
图像image2=新图像(显示器,20,20);
颜色=display.getSystemColor(SWT.color\u绿色);
gc=新gc(图2);
gc.setBackground(颜色);
gc.fillRectangle(image2.getBounds());
gc.dispose();
工具栏条=空;
如果(使用ShellToolbar){
bar=shell.getToolBar();
}否则{
bar=新工具栏(外壳,SWT.BORDER | SWT.FLAT);
}
矩形clientArea=shell.getClientArea();
条形立根(clientArea.x,clientArea.y,200,32);
整数=3;
最终工具项[]项=新工具项[编号];
对于(int i=0;i

您需要对每个按钮都执行此操作,然后将全部取消选择,尝试使用
buttonname.optionName(optionargs)进行体验

全部取消选择?一次只能在按钮组中选择一个单选按钮…如果希望全部取消选择,则需要使用复选框或其他控件。单选按钮在分组时始终具有固有的选定按钮。我是SWT中的新手,但toolItem.setSelection(false)迭代无线电项目似乎对我有用。请粘贴一些代码。我使用Juno附带的SWT版本。不,没有使用任何组。组可以用于工具栏中的ToolItems吗?
// taken from SWT Snippet47
package org.eclipse.swt.snippets;

import org.eclipse.swt.*;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.widgets.*;

public class ToolbarRadioButtonGroupTest {

public static void main (String [] args) {

    boolean useShellToolBar  = true;

    Display display = new Display ();
    Shell shell = new Shell (display);

    Image image = new Image (display, 20, 20);
    Color color = display.getSystemColor (SWT.COLOR_BLUE);
    GC gc = new GC (image);
    gc.setBackground (color);
    gc.fillRectangle (image.getBounds ());
    gc.dispose ();

    Image image2 = new Image (display, 20, 20);
    color = display.getSystemColor (SWT.COLOR_GREEN);
    gc = new GC (image2);
    gc.setBackground (color);
    gc.fillRectangle (image2.getBounds ());
    gc.dispose ();

    ToolBar bar = null;
    if (useShellToolBar){
        bar = shell.getToolBar();
    }else{
        bar = new ToolBar (shell, SWT.BORDER | SWT.FLAT);
    }

    Rectangle clientArea = shell.getClientArea ();
    bar.setBounds (clientArea.x, clientArea.y, 200, 32);
    int number = 3;
    final ToolItem[] items = new ToolItem[number];
    for (int i=0; i<number; i++) {
        ToolItem item = new ToolItem (bar, SWT.RADIO);
        item.setImage (image);
        items[i] = item;
    }

    ToolItem sep = new ToolItem(bar, SWT.SEPARATOR);

    ToolItem push = new ToolItem(bar, SWT.PUSH);
    push.setImage(image2);
    push.addListener(SWT.Selection, new Listener(){

        @Override
        public void handleEvent(Event event) {
            for (ToolItem toolItem : items) {
                toolItem.setSelection(false);
            }

        }

    });

    shell.open ();
    while (!shell.isDisposed ()) {
        if (!display.readAndDispatch ()) display.sleep ();
    }
    image.dispose ();
    image2.dispose ();
    display.dispose ();
}
} 
btnB.setSelection(false);