将Eclipse命令绑定到swt按钮

将Eclipse命令绑定到swt按钮,eclipse,command,swt,Eclipse,Command,Swt,在Eclipse中,您可以使用菜单贡献来添加工具栏按钮和将调用命令的菜单。除了以编程方式调用onclick命令外,还有其他方法可以对普通swt按钮执行此操作吗?否。您必须侦听按钮事件并以编程方式调用命令 您可以在这样的视图或向导中使用CommandContributionItems: button.addSelectionListener(new SelectionAdapter() { @Override public void widgetSelected(Selectio

在Eclipse中,您可以使用菜单贡献来添加工具栏按钮和将调用命令的菜单。除了以编程方式调用onclick命令外,还有其他方法可以对普通swt按钮执行此操作吗?

否。您必须侦听按钮事件并以编程方式调用命令

您可以在这样的视图或向导中使用CommandContributionItems:

button.addSelectionListener(new SelectionAdapter() {

    @Override
    public void widgetSelected(SelectionEvent e) {
        IHandlerService handlerService = (IHandlerService) getSite()
                .getService(IHandlerService.class);
        try {
            handlerService.executeCommand("my command id", null);
        } catch (Exception ex) {
            throw new RuntimeException("command with id \"my command id\" not found");
        }

    }
});
CommandContributionItemParameter param = new CommandContributionItemParameter(getSite(),
            "myCommand", "com.voo.myCommand", CommandContributionItem.STYLE_PUSH);
param.label = "My Label";
CommandContributionItem item = new CommandContributionItem(param);
item.fill(parent);

获得句柄服务的其他方法。IHandlerService handlerService=(IHandlerService)PlatformUI.getWorkbench().getService(IHandlerService.class);