Draggable toolbar

孤街醉人 提交于 2019-12-10 10:39:22

问题


how to make draggable/dockable toolbar with JFace/SWT like Eclipse has? Could you post a simple example of ApplicationWindow or link good source of how to make it.

Thanks.


回答1:


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




回答2:


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


来源:https://stackoverflow.com/questions/5366772/draggable-toolbar

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