public static void main(String[] args) {
    Shell shell = new Shell();
    Display display = shell.getDisplay();
    shell.setLayout(new FillLayout());
    shell.setText("ExpandBar Example");

    Menu menubar = new Menu(shell, SWT.BAR);
    shell.setMenuBar(menubar);
    MenuItem fileItem = new MenuItem(menubar, SWT.CASCADE);
    fileItem.setText("&File");
    Menu submenu = new Menu(shell, SWT.DROP_DOWN);
    fileItem.setMenu(submenu);
    item = new MenuItem(submenu, SWT.PUSH);
    item.setText("New ExpandItem");

    bar = new ExpandBar(shell, SWT.V_SCROLL);
    image = display.getSystemImage(SWT.ICON_QUESTION);

    // First item
    Composite composite = new Composite(bar, SWT.NONE);
    Menu popupmenu = new Menu(shell, SWT.POP_UP);
    MenuItem popupItem = new MenuItem(popupmenu, SWT.PUSH);
    popupItem.setText("Popup");
    composite.setMenu(popupmenu);

    GridLayout layout = new GridLayout(2, false);
    layout.marginLeft = layout.marginTop = layout.marginRight = layout.marginBottom = 10;
    layout.verticalSpacing = 10;
    composite.setLayout(layout);
    Label label = new Label(composite, SWT.NONE);
    label.setImage(display.getSystemImage(SWT.ICON_ERROR));
    label = new Label(composite, SWT.NONE);
    label.setText("SWT.ICON_ERROR");
    label = new Label(composite, SWT.NONE);
    label.setImage(display.getSystemImage(SWT.ICON_INFORMATION));
    label = new Label(composite, SWT.NONE);
    label.setText("SWT.ICON_INFORMATION");
    label = new Label(composite, SWT.NONE);
    label.setImage(display.getSystemImage(SWT.ICON_WARNING));
    label = new Label(composite, SWT.NONE);
    label.setText("SWT.ICON_WARNING");
    label = new Label(composite, SWT.NONE);
    label.setImage(display.getSystemImage(SWT.ICON_QUESTION));
    label = new Label(composite, SWT.NONE);
    label.setText("SWT.ICON_QUESTION");
    ExpandItem item1 = new ExpandItem(bar, SWT.NONE);
    item1.setText("What is your favorite icon");
    item1.setHeight(composite.computeSize(SWT.DEFAULT, SWT.DEFAULT).y);
    item1.setControl(composite);
    item1.setImage(image);

    // Second item
    composite = new Composite(bar, SWT.NONE);
    layout = new GridLayout(2, true);
    layout.marginLeft = layout.marginTop = layout.marginRight = layout.marginBottom = 10;
    layout.verticalSpacing = 10;
    composite.setLayout(layout);
    Button button1 = new Button(composite, SWT.PUSH);
    button1.setText("Button 1");
    Button button2 = new Button(composite, SWT.PUSH);
    button2.setText("Button 2");

    ExpandItem item0 = new ExpandItem(bar, SWT.NONE);
    item0.setText("What is your favorite button");
    item0.setHeight(composite.computeSize(SWT.DEFAULT, SWT.DEFAULT).y);
    item0.setControl(composite);
    item0.setImage(image);
    item0.setExpanded(false);

    item.addListener(
        SWT.Selection,
        new Listener() {
          @Override
          public void handleEvent(Event e) {
            ExpandItem item2 = new ExpandItem(bar, SWT.NONE);
            Composite composite = new Composite(bar, SWT.NONE);
            GridLayout layout = new GridLayout(2, false);
            composite.setLayout(layout);
            Label label = new Label(composite, SWT.NONE);
            label.setText("What is your name?");
            Composite pcomposite = new Composite(composite, SWT.NONE);
            Text text = new Text(pcomposite, SWT.NONE);
            item2.setText("New Question");
            text.pack();
            composite.pack();
            Point size = composite.computeSize(SWT.DEFAULT, SWT.DEFAULT);
            item2.setHeight(size.y);
            item2.setControl(composite);
            item2.setImage(image);
            item2.setExpanded(true);
          }
        });

    bar.setSpacing(8);
    shell.setSize(400, 550);
    shell.open();
    System.out.println("------------------------------");
    System.out.println("getSize: " + button1.getSize());
    System.out.println("getBounds: " + button1.getBounds());
    System.out.println("computeSize: " + button1.computeSize(SWT.DEFAULT, SWT.DEFAULT));
    System.out.println("getLocation: " + button1.getLocation());

    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
    image.dispose();
    display.dispose();
  }
示例#2
0
  /** Initializes the GUI. */
  private void initGUI() {
    try {
      getShell()
          .addDisposeListener(
              new DisposeListener() {
                public void widgetDisposed(DisposeEvent evt) {
                  shellWidgetDisposed(evt);
                }
              });

      getShell()
          .addControlListener(
              new ControlAdapter() {
                public void controlResized(ControlEvent evt) {
                  shellControlResized(evt);
                }
              });

      getShell()
          .addControlListener(
              new ControlAdapter() {
                public void controlMoved(ControlEvent evt) {
                  shellControlMoved(evt);
                }
              });

      GridLayout thisLayout = new GridLayout();
      this.setLayout(thisLayout);
      {
        GridData toolBarLData = new GridData();
        toolBarLData.grabExcessHorizontalSpace = true;
        toolBarLData.horizontalAlignment = GridData.FILL;
        toolBar = new ToolBar(this, SWT.FLAT);
        toolBar.setLayoutData(toolBarLData);
        toolBar.setBackgroundImage(SWTResourceManager.getImage("images/ToolbarBackground.gif"));
        {
          newToolItem = new ToolItem(toolBar, SWT.NONE);
          newToolItem.setImage(SWTResourceManager.getImage("images/new.gif"));
          newToolItem.setToolTipText("New");
        }
        {
          openToolItem = new ToolItem(toolBar, SWT.NONE);
          openToolItem.setToolTipText("Open");
          openToolItem.setImage(SWTResourceManager.getImage("images/open.gif"));
          openToolItem.addSelectionListener(
              new SelectionAdapter() {
                public void widgetSelected(SelectionEvent evt) {
                  openToolItemWidgetSelected(evt);
                }
              });
        }
        {
          saveToolItem = new ToolItem(toolBar, SWT.NONE);
          saveToolItem.setToolTipText("Save");
          saveToolItem.setImage(SWTResourceManager.getImage("images/save.gif"));
        }
      }
      {
        clientArea = new Composite(this, SWT.NONE);
        GridData clientAreaLData = new GridData();
        clientAreaLData.grabExcessHorizontalSpace = true;
        clientAreaLData.grabExcessVerticalSpace = true;
        clientAreaLData.horizontalAlignment = GridData.FILL;
        clientAreaLData.verticalAlignment = GridData.FILL;
        clientArea.setLayoutData(clientAreaLData);
        clientArea.setLayout(null);
      }
      {
        statusArea = new Composite(this, SWT.NONE);
        GridLayout statusAreaLayout = new GridLayout();
        statusAreaLayout.makeColumnsEqualWidth = true;
        statusAreaLayout.horizontalSpacing = 0;
        statusAreaLayout.marginHeight = 0;
        statusAreaLayout.marginWidth = 0;
        statusAreaLayout.verticalSpacing = 0;
        statusAreaLayout.marginLeft = 3;
        statusAreaLayout.marginRight = 3;
        statusAreaLayout.marginTop = 3;
        statusAreaLayout.marginBottom = 3;
        statusArea.setLayout(statusAreaLayout);
        GridData statusAreaLData = new GridData();
        statusAreaLData.horizontalAlignment = GridData.FILL;
        statusAreaLData.grabExcessHorizontalSpace = true;
        statusArea.setLayoutData(statusAreaLData);
        statusArea.setBackground(SWTResourceManager.getColor(239, 237, 224));
        {
          statusText = new Label(statusArea, SWT.BORDER);
          statusText.setText(" Ready");
          GridData txtStatusLData = new GridData();
          txtStatusLData.horizontalAlignment = GridData.FILL;
          txtStatusLData.grabExcessHorizontalSpace = true;
          txtStatusLData.verticalIndent = 3;
          statusText.setLayoutData(txtStatusLData);
        }
      }
      thisLayout.verticalSpacing = 0;
      thisLayout.marginWidth = 0;
      thisLayout.marginHeight = 0;
      thisLayout.horizontalSpacing = 0;
      thisLayout.marginTop = 3;
      this.setSize(474, 312);
      {
        menu1 = new Menu(getShell(), SWT.BAR);
        getShell().setMenuBar(menu1);
        {
          fileMenuItem = new MenuItem(menu1, SWT.CASCADE);
          fileMenuItem.setText("&File");
          {
            fileMenu = new Menu(fileMenuItem);
            {
              newFileMenuItem = new MenuItem(fileMenu, SWT.PUSH);
              newFileMenuItem.setText("&New");
              newFileMenuItem.setImage(SWTResourceManager.getImage("images/new.gif"));
            }
            {
              openFileMenuItem = new MenuItem(fileMenu, SWT.PUSH);
              openFileMenuItem.setText("&Open");
              openFileMenuItem.setImage(SWTResourceManager.getImage("images/open.gif"));
              openFileMenuItem.addSelectionListener(
                  new SelectionAdapter() {
                    public void widgetSelected(SelectionEvent evt) {
                      openFileMenuItemWidgetSelected(evt);
                    }
                  });
            }
            {
              closeFileMenuItem = new MenuItem(fileMenu, SWT.CASCADE);
              closeFileMenuItem.setText("Close");
            }
            {
              fileMenuSep1 = new MenuItem(fileMenu, SWT.SEPARATOR);
            }
            {
              saveFileMenuItem = new MenuItem(fileMenu, SWT.PUSH);
              saveFileMenuItem.setText("&Save");
              saveFileMenuItem.setImage(SWTResourceManager.getImage("images/save.gif"));
              saveFileMenuItem.addSelectionListener(
                  new SelectionAdapter() {
                    public void widgetSelected(SelectionEvent evt) {
                      saveFileMenuItemWidgetSelected(evt);
                    }
                  });
            }
            {
              fileMenuSep2 = new MenuItem(fileMenu, SWT.SEPARATOR);
            }
            {
              exitMenuItem = new MenuItem(fileMenu, SWT.CASCADE);
              exitMenuItem.setText("E&xit");
              exitMenuItem.addSelectionListener(
                  new SelectionAdapter() {
                    public void widgetSelected(SelectionEvent evt) {
                      exitMenuItemWidgetSelected(evt);
                    }
                  });
            }
            fileMenuItem.setMenu(fileMenu);
          }
        }
        {
          helpMenuItem = new MenuItem(menu1, SWT.CASCADE);
          helpMenuItem.setText("&Help");
          {
            helpMenu = new Menu(helpMenuItem);
            {
              aboutMenuItem = new MenuItem(helpMenu, SWT.CASCADE);
              aboutMenuItem.setText("&About");
              aboutMenuItem.addSelectionListener(
                  new SelectionAdapter() {
                    public void widgetSelected(SelectionEvent evt) {
                      aboutMenuItemWidgetSelected(evt);
                    }
                  });
            }
            helpMenuItem.setMenu(helpMenu);
          }
        }
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }