簡體   English   中英

如何使用具有下拉功能的SWT.RADIO樣式創建eclipse工具欄項?

[英]How do I create an eclipse toolbar item with SWT.RADIO style with drop-down capabilities?

在eclipse SWT中,當你可以為控件添加多個樣式時它會派上用場。 工具欄可以添加多個樣式。 工具項目不能享有同樣的特權嗎? 圍繞解決方案的工作是什么?

ToolItem API清楚地說明了這一點

只能指定CHECK,PUSH,RADIO,SEPARATOR和DROP_DOWN樣式中的一種。

基本上,我希望有一個工具欄項,就像一個帶下拉列表的單選按鈕。 我希望用戶能夠從其下拉列表中的項目列表中更改項目的默認操作。

任何人都可以指出我正確的方向嗎?

    ...
    ToolBar toolBar = new ToolBar(composite, SWT.FLAT | SWT.RIGHT | SWT.HORIZONTAL);

    ToolItem item1 = new ToolItem(toolBar, SWT.RADIO);
    item1.addSelectionListener(new SelectionAdapter() {
        @Override
        public void widgetSelected(SelectionEvent e) {
            // do something
        }
    });
    item1.setImage(image1);

    ToolItem item2 = new ToolItem(toolBar, SWT.RADIO | STW.DROP_DOWN); //only allowed in my dreams
    item2.addSelectionListener(new SelectionAdapter() {
        @Override
        public void widgetSelected(SelectionEvent e) {
            // do a drop down action and lots more.
            // change image of this ToolItem to match the drop down selection.
            item2.setImage(selectedImage);
        }

    });
    item2.setImage(image2);

代碼段顯示了如何創建下拉菜單。 要獲取單選按鈕,請使用SWT.RADIO樣式而不是SWT.PUSH作為MenuItems。

final ToolBar toolBar = new ToolBar (shell, SWT.NONE);
Rectangle clientArea = shell.getClientArea ();
toolBar.setLocation(clientArea.x, clientArea.y);
final Menu menu = new Menu (shell, SWT.POP_UP);
for (int i=0; i<8; i++) {
    MenuItem item = new MenuItem (menu, SWT.PUSH);
item.setText ("Item " + i);
}
final ToolItem item = new ToolItem (toolBar, SWT.DROP_DOWN);
item.addListener (SWT.Selection, new Listener () {
    public void handleEvent (Event event) {
        if (event.detail == SWT.ARROW) {
        Rectangle rect = item.getBounds ();
    Point pt = new Point (rect.x, rect.y + rect.height);
    pt = toolBar.toDisplay (pt);
    menu.setLocation (pt.x, pt.y);
    menu.setVisible (true);
        }
}
});
toolBar.pack ();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM