我在工具栏中有几个样式为 SWT.RADIO
的 SWT ToolItems
。我想使用 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 ();
}
}
最佳答案
btnB.setSelection(false);
你需要对每个按钮都这样做,然后你将取消选择它们,尝试使用 buttonname.optionName(optionargs);
关于java - 取消选择组的所有 SWT 单选按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16780670/