java swt 菜单_SWT 给工具栏关联弹出菜单的工具类

本文介绍如何使用SWT Util工具类将弹出菜单关联到工具栏上的ToolItem,通过监听器在选中时显示菜单。示例代码来源于SWT Snippets,适用于纯SWT环境。

从 SWT Snippets 里提取出来的, 虽然 JFace 里有更好的 MenuManager 之类的, 但是这个对 SWT 就可以用了.

import org.eclipse.swt.events.SelectionEvent;

import org.eclipse.swt.events.SelectionListener;

import org.eclipse.swt.graphics.Point;

import org.eclipse.swt.graphics.Rectangle;

import org.eclipse.swt.widgets.Menu;

import org.eclipse.swt.widgets.ToolItem;

public class SWTUtil {

/**

* Assign a popup menu to a widget.

* Whenever the widget is selected, the popup menu will displayed.

* @param parent

* @param popupMenu

*/

public static void assignPopupMenuToWidget(final ToolItem parent, final Menu popupMenu) {

parent.addSelectionListener(new SelectionListener() {

public void widgetDefaultSelected(SelectionEvent e) {

widgetSelected(e);

}

public void widgetSelected(SelectionEvent evt) {

// if (evt.detail == SWT.ARROW) {

Rectangle b = parent.getBounds();

Point pt = new Point(b.x, b.y + b.height);

pt = parent.getParent().toDisplay(pt);

popupMenu.setLocation(pt.x, pt.y);

popupMenu.setVisible(true);

// }

}

});

}

}

posted on 2007-10-10 13:47 gembin 阅读(440) 评论(0)  编辑  收藏

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值