java - 带下拉菜单的可点击按钮

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

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

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

我希望该按钮的行为类似于 Debug\Run\New 图标按钮:可通过侧面的下拉菜单进行点击。

enter image description here

最佳答案

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

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()
                                }
                            });

关于java - 带下拉菜单的可点击按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56018931/

相关文章:

java - 尝试创建存储库实例时出现 org.springframework.data.mapping.MappingException

android - 如何在 Eclipse 中为 android 的 google drive api 设置 Java Doc

javascript - 使用 Javascript 获取下拉列表中选择的值(显示 null)

javascript - 如何使用 Material UI 在 React JS 中实现下拉菜单?

javascript - 单选按钮无法返回不同状态

java - 具有可变数量 @XMLElements 的 JAXB

ArrayList 的 java.lang.IndexOutOfBoundsException

java - 如何构建不同项目共享的不同通用代码?

php - sql 查询返回一个值,但 php 上的 $result 始终是一个空数组

javascript - 使用变量指定选择框获取更改选择框的选定值