【发布时间】:2013-05-22 18:03:45
【问题描述】:
我在工具栏中有几个 SWT ToolItems 样式为 SWT.RADIO。我想使用toolItem.setSelection(false) 以编程方式取消选择所有这些,但这是不可能的。当前选中的ToolItem 保持选中状态。我可以使用toolItem.setSelection(true) 选择另一个 ToolItem,这将取消选择第一个,但我找不到取消选择所有这些的方法,尽管当 GUI 启动时,它们都被取消选择。有谁知道如何取消选择所有这些?
更新:我发现了问题所在:我检查了@rgeorge,发现toolItem.setSelection(false) 可以工作,如果工具栏是用ToolBar toolBar = new ToolBar(shell) 构造的。但是如果我在Mac上使用shell.getToolBar()可以得到的工具栏(窗框统一工具栏)就不行了。似乎是 SWT 不兼容。这是一些重现效果的代码(更改useShellToolbar以在Mac上切换案例):
// 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 ();
}
}
【问题讨论】:
-
取消全选?一次只能在按钮组中选择一个单选按钮...
-
如果您希望全部取消选中,您需要使用复选框或其他控件。分组后的单选按钮总是有一个固有的选择按钮。
-
我是 SWT 的新手,但 toolItem.setSelection(false) 迭代无线电项目似乎对我有用。请粘贴一些代码。我使用的是 Juno 自带的 SWT 版本。
-
不,没有使用任何组。组可以用于工具栏中的工具项吗?
标签: java eclipse radio-button swt radio-group