简体   繁体   English

如何使用具有下拉功能的SWT.RADIO样式创建eclipse工具栏项?

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

In eclipse SWT land, it comes in handy when you can add multiple styles to controls. 在eclipse SWT中,当你可以为控件添加多个样式时它会派上用场。 Toolbars can have multiple styles added. 工具栏可以添加多个样式。 Can't tool items enjoy the same privilege? 工具项目不能享有同样的特权吗? What is the work around solution? 围绕解决方案的工作是什么?

The ToolItem API clearly states that ToolItem API清楚地说明了这一点

Only one of the styles CHECK, PUSH, RADIO, SEPARATOR and DROP_DOWN may be specified. 只能指定CHECK,PUSH,RADIO,SEPARATOR和DROP_DOWN样式中的一种。

Basically, I would like to have a toolbar item to behave like a radio button with a drop down. 基本上,我希望有一个工具栏项,就像一个带下拉列表的单选按钮。 I want users to be able to change an item's default action from a list of items in its drop down. 我希望用户能够从其下拉列表中的项目列表中更改项目的默认操作。

Can anyone kindly point me in the right direction? 任何人都可以指出我正确的方向吗?

    ...
    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);

This snippet shows how to create a drop down menu. 代码段显示了如何创建下拉菜单。 To get the radio buttons, use the SWT.RADIO style instead of SWT.PUSH for the MenuItems. 要获取单选按钮,请使用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