Draggable toolbar

假装没事ソ 提交于 2019-12-06 04:33:41

SWT has a component called CoolBar, You can create CoolBars fairly easily by using CoolBarManager, or you can manually use just them (API Doc)

In case that someone found this question I have prepared small example. My problem was in incorrect use of add method. You have to use add(IToolBarManager toolBarManager) method from CoolBarManager not one of from base class ContributionManager.

import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.CoolBarManager;
import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.jface.action.ToolBarManager;
import org.eclipse.jface.window.ApplicationWindow;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class App extends ApplicationWindow {

  public App(Shell parent) {
    super(parent);
  }

  @Override
  protected Control createContents(Composite parent) {
    getShell().setText("CoolBarManager example");

    return super.createContents(parent);
  }

  @Override
  public void create() {
    addCoolBar(SWT.FLAT);
    super.create();
  }

  @Override
  protected CoolBarManager createCoolBarManager(int style) {
    CoolBarManager cbm = new CoolBarManager(style);

    IToolBarManager tb1 = new ToolBarManager(style);
    IToolBarManager tb2 = new ToolBarManager(style);

    tb1.add(new Action() {
      {
        setText("&Button1");
      }
    });
    tb1.add(new Action() {
      {
        setText("&Button2");
      }
    });
    tb1.add(new Action() {
      {
        setText("&Button3");
      }
    });

    tb2.add(new Action() {
      {
        setText("&Button4");
      }
    });

    tb2.add(new Action() {
      {
        setText("&Button5");
      }
    });

    cbm.add(tb1);
    cbm.add(tb2);

    return cbm;
  }

  public static void main(String[] args) {
    App app = new App(null);

    app.setBlockOnOpen(true);
    app.open();

    Display.getCurrent().dispose();
  }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!