[英]Deselect all SWT radio buttons of a group
我在工具欄中有幾個樣式為SWT.RADIO
的SWT ToolItems
。 我想使用toolItem.setSelection(false)
以編程方式取消選擇所有這些toolItem.setSelection(false)
,但這是不可能的。 當前選擇的ToolItem
保持選中狀態。 我可以使用toolItem.setSelection(true)
選擇另一個ToolItem,這將取消選擇第一個,但是我找不到找到取消所有選擇的方法,盡管在GUI啟動時,所有選擇都被取消了。 有誰知道如何取消所有選擇?
更新:我發現了問題所在:如果工具欄由ToolBar toolBar = new ToolBar(shell)
構造,我檢查了@rgeorge,發現toolItem.setSelection(false)
可以工作。 但是,如果我使用Mac上的shell.getToolBar()
可以獲取的工具欄(窗口框架統一工具欄),則無法使用。 似乎與SWT不兼容。 這是一些重現效果的代碼(更改useShellToolbar
在Mac上的useShellToolbar
之間進行切換):
// 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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.