带下拉菜单的可点击按钮
Clickable button with drop down
我正在尝试创建一个具有下拉菜单且可点击的按钮。
在开发 eclipse 插件时我该怎么做?
如果可能的话,我正在寻找特定于 Eclipse 插件开发的答案。最好没有 xmls,只有 java 代码。
搜索并检查了 eclipse 文档,但没有找到执行这两项操作的按钮的可用答案。希望有 link 在线示例(猜测它存在)。
我希望按钮的行为类似于 Debug\Run\New 图标按钮:可点击,旁边有下拉菜单。
最后自己找到了迂回的办法。
这是一个例子:
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()
}
});
我正在尝试创建一个具有下拉菜单且可点击的按钮。 在开发 eclipse 插件时我该怎么做? 如果可能的话,我正在寻找特定于 Eclipse 插件开发的答案。最好没有 xmls,只有 java 代码。
搜索并检查了 eclipse 文档,但没有找到执行这两项操作的按钮的可用答案。希望有 link 在线示例(猜测它存在)。
我希望按钮的行为类似于 Debug\Run\New 图标按钮:可点击,旁边有下拉菜单。
最后自己找到了迂回的办法。 这是一个例子:
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()
}
});