【问题标题】:Clickable button with drop down带有下拉菜单的可点击按钮
【发布时间】:2019-05-07 08:55:32
【问题描述】:

我正在尝试创建一个具有下拉菜单且可点击的按钮。 在为 eclipse 插件开发时我将如何做到这一点? 如果可能,我正在寻找特定于 Eclipse 插件开发的答案。最好没有xmls,只有java代码。

搜索并检查了 Eclipse 文档,但没有找到执行两者的按钮的可访问答案。希望有一个在线示例的链接(猜测它存在)。

我希望按钮的行为类似于 Debug\Run\New 图标按钮:可点击,旁边有下拉菜单。

【问题讨论】:

  • 有一些信息here
  • @greg-449 感谢您提供的信息,但我已经看到了,这不是我想要的。我需要一个基于 java 的解决方案,因为我正试图摆脱那些可怕的 xmls...
  • 扩展点 XML 是做这类事情的正确方法。

标签: java eclipse drop-down-menu eclipse-plugin dropdown


【解决方案1】:

最后自己找到了一个迂回的解决方案。 这是一个例子:

import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;
import org.eclipse.swt.SWT;
....
              //These 3 lines are the solution for style, turns out to be simple.
                Image image = <<Your image for deselected>>;
                ToolItem item = = new ToolItem(toolBar, SWT.BEGINNING | SWT.DROP_DOWN);
                item.setImage(image);
              //Menu creation for arrow
                Menu menu = new Menu(item.getParent().getShell());
                new MenuItem(menu, SWT.PUSH).setText("Menu item example 1");
              //Indication for state of button press (to give it checkbox behavior - does not have to be atomic)
                AtomicBoolean recording = new AtomicBoolean(false);
              //This is where the logic is for opening the menu and enabling a checkbox behavior 
              //is done. ((width * 3) was added because tool bar is on the side for my specific use case)
                item.addListener(SWT.Selection, e -> {
                                if (e.detail == SWT.ARROW) {
                                    Rectangle rect = item.getBounds();
                                    Point pt = new Point(rect.x - (rect.width * 3), rect.y + rect.height);
                                    pt = toolBar.toDisplay(pt);
                                    menu.setLocation(pt.x, pt.y);
                                    menu.setVisible(true);
                                } else {
                                   //logic for pressing icon. You can swap the icon 
                                   //image according to needed with item.setImage()
                                }
                            });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-06
    • 2018-07-27
    • 2018-09-16
    • 2015-03-27
    • 1970-01-01
    • 2022-12-31
    相关资源
    最近更新 更多