Java/SWT JFace Eclipse/ToolBar

Материал из Java эксперт
Версия от 05:57, 1 июня 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Create a flat tool bar (images)

/*
 * ToolBar example snippet: create a flat tool bar (images)
 *
 * For a list of all SWT example snippets see
 * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets
 */
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;
public class Snippet36 {
  public static void main(String[] args) {
    Display display = new Display();
    Image image = new Image(display, 16, 16);
    Color color = display.getSystemColor(SWT.COLOR_RED);
    GC gc = new GC(image);
    gc.setBackground(color);
    gc.fillRectangle(image.getBounds());
    gc.dispose();
    Shell shell = new Shell(display);
    ToolBar toolBar = new ToolBar(shell, SWT.FLAT | SWT.BORDER);
    for (int i = 0; i < 12; i++) {
      ToolItem item = new ToolItem(toolBar, SWT.DROP_DOWN);
      item.setImage(image);
    }
    toolBar.pack();
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }
    image.dispose();
    display.dispose();
  }
}





Create a tool bar (text)

/*
 * ToolBar example snippet: create a tool bar (text)
 *
 * For a list of all SWT example snippets see
 * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets
 */
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;
public class Snippet18 {
  public static void main(String[] args) {
    Shell shell = new Shell();
    ToolBar bar = new ToolBar(shell, SWT.BORDER);
    for (int i = 0; i < 8; i++) {
      ToolItem item = new ToolItem(bar, SWT.PUSH);
      item.setText("Item " + i);
    }
    bar.pack();
    shell.open();
    Display display = shell.getDisplay();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }
    display.dispose();
  }
}





Creates a toolbar

//Send questions, comments, bug reports, etc. to the authors:
//Rob Warner (rwarner@interspatial.ru)
//Robert Harris (rbrt_harris@yahoo.ru)
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.*;
/**
 * This class creates a toolbar.
 */
public class SWTToolBarTest {
  public static void main(String[] args) {
    new SWTToolBarTest().run();
  }
  public void run() {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("Toolbar Test");
    shell.setLayout(new FillLayout());
    createToolbar(shell);
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
    display.dispose();
  }
  public void createToolbar(Shell shell) {
    ToolBar toolBar = new ToolBar(shell, SWT.HORIZONTAL);
    ToolItem item = new ToolItem(toolBar, SWT.PUSH);
    item.setText("Button One");
    item = new ToolItem(toolBar, SWT.PUSH);
    item.setText("Button Two");
    new ToolItem(toolBar, SWT.SEPARATOR);
    item = new ToolItem(toolBar, SWT.CHECK);
    item.setText("Check One");
    item = new ToolItem(toolBar, SWT.CHECK);
    item.setText("Check Two");
    new ToolItem(toolBar, SWT.SEPARATOR);
    item = new ToolItem(toolBar, SWT.RADIO);
    item.setText("Radio One");
    item = new ToolItem(toolBar, SWT.RADIO);
    item.setText("Radio Two");
    new ToolItem(toolBar, SWT.SEPARATOR);
    item = new ToolItem(toolBar, SWT.DROP_DOWN);
    item.setText("Dropdown One");
    item = new ToolItem(toolBar, SWT.DROP_DOWN);
    item.setText("Dropdown Two");
  }
}





Create tool bar (normal, hot and disabled images)

/*
 * ToolBar example snippet: create tool bar (normal, hot and disabled images)
 *
 * For a list of all SWT example snippets see
 * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets
 */
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;
public class Snippet47 {
public static void main (String [] args) {
  Display display = new Display ();
  Shell shell = new Shell (display);
  Image image = new Image (display, 20, 20);
  Color color = display.getSystemColor (SWT.COLOR_BLUE);
  GC gc = new GC (image);
  gc.setBackground (color);
  gc.fillRectangle (image.getBounds ());
  gc.dispose ();
  
  Image disabledImage = new Image (display, 20, 20);
  color = display.getSystemColor (SWT.COLOR_GREEN);
  gc = new GC (disabledImage);
  gc.setBackground (color);
  gc.fillRectangle (disabledImage.getBounds ());
  gc.dispose ();
  
  Image hotImage = new Image (display, 20, 20);
  color = display.getSystemColor (SWT.COLOR_RED);
  gc = new GC (hotImage);
  gc.setBackground (color);
  gc.fillRectangle (hotImage.getBounds ());
  gc.dispose ();
  
  ToolBar bar = new ToolBar (shell, SWT.BORDER | SWT.FLAT);
  bar.setSize (200, 32);
  for (int i=0; i<12; i++) {
    ToolItem item = new ToolItem (bar, 0);
    item.setImage (image);
    item.setDisabledImage (disabledImage);
    item.setHotImage (hotImage);
    if (i % 3 == 0) item.setEnabled (false);
  }
  
  shell.open ();
  bar.getItems () [1].setImage (disabledImage);
  while (!shell.isDisposed ()) {
    if (!display.readAndDispatch ()) display.sleep ();
  }
  image.dispose ();
  disabledImage.dispose ();
  hotImage.dispose ();
  display.dispose ();
}
}





Create tool bar (wrap on resize)

/*
 * ToolBar example snippet: create tool bar (wrap on resize)
 *
 * For a list of all SWT example snippets see
 * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets
 */
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;
public class Snippet49 {
  public static void main(String[] args) {
    Display display = new Display();
    final Shell shell = new Shell(display);
    final ToolBar toolBar = new ToolBar(shell, SWT.WRAP);
    for (int i = 0; i < 12; i++) {
      ToolItem item = new ToolItem(toolBar, SWT.PUSH);
      item.setText("Item " + i);
    }
    shell.addListener(SWT.Resize, new Listener() {
      public void handleEvent(Event e) {
        Rectangle rect = shell.getClientArea();
        Point size = toolBar.ruputeSize(rect.width, SWT.DEFAULT);
        toolBar.setSize(size);
      }
    });
    toolBar.pack();
    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }
    display.dispose();
  }
}





Place a combo box in a tool bar

/*
 * ToolBar example snippet: place a combo box in a tool bar
 *
 * For a list of all SWT example snippets see
 * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets
 */
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.rubo;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;
public class Snippet58 {
  public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    ToolBar bar = new ToolBar(shell, SWT.BORDER);
    for (int i = 0; i < 4; i++) {
      ToolItem item = new ToolItem(bar, 0);
      item.setText("Item " + i);
    }
    ToolItem sep = new ToolItem(bar, SWT.SEPARATOR);
    int start = bar.getItemCount();
    for (int i = start; i < start + 4; i++) {
      ToolItem item = new ToolItem(bar, 0);
      item.setText("Item " + i);
    }
    Combo combo = new Combo(bar, SWT.READ_ONLY);
    for (int i = 0; i < 4; i++) {
      combo.add("Item " + i);
    }
    combo.pack();
    sep.setWidth(combo.getSize().x);
    sep.setControl(combo);
    bar.pack();
    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }
    display.dispose();
  }
}





Place a drop down menu in a tool bar

/*
 * ToolBar example snippet: Place a drop down menu in a tool bar
 *
 * For a list of all SWT example snippets see
 * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets
 */
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;
public class Snippet67 {
  public static void main(String[] args) {
    final Display display = new Display();
    final Shell shell = new Shell(display);
    final ToolBar toolBar = new ToolBar(shell, SWT.NONE);
    final Menu menu = new Menu(shell, SWT.POP_UP);
    for (int i = 0; i < 8; i++) {
      MenuItem item = new MenuItem(menu, SWT.PUSH);
      item.setText("Item " + i);
    }
    final ToolItem item = new ToolItem(toolBar, SWT.DROP_DOWN);
    item.addListener(SWT.Selection, new Listener() {
      public void handleEvent(Event event) {
        if (event.detail == SWT.ARROW) {
          Rectangle rect = item.getBounds();
          Point pt = new Point(rect.x, rect.y + rect.height);
          pt = toolBar.toDisplay(pt);
          menu.setLocation(pt.x, pt.y);
          menu.setVisible(true);
        }
      }
    });
    toolBar.pack();
    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }
    menu.dispose();
    display.dispose();
  }
}





SWT Toolbar Demo

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;
public class ToolbarClass {
  public static void main(String[] args) {
    Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setSize(300, 200);
    shell.setText("Toolbar Example");
    ToolBar toolbar = new ToolBar(shell, SWT.NONE);
    toolbar.setBounds(0, 0, 200, 70);
    ToolItem toolItem1 = new ToolItem(toolbar, SWT.PUSH);
    toolItem1.setText("Save");
    ToolItem toolItem2 = new ToolItem(toolbar, SWT.PUSH);
    toolItem2.setText("Save As");
    ToolItem toolItem3 = new ToolItem(toolbar, SWT.PUSH);
    toolItem3.setText("Print");
    ToolItem toolItem4 = new ToolItem(toolbar, SWT.PUSH);
    toolItem4.setText("Run");
    ToolItem toolItem5 = new ToolItem(toolbar, SWT.PUSH);
    toolItem5.setText("Help");
    final Text text = new Text(shell, SWT.BORDER);
    text.setBounds(55, 80, 200, 25);
    Listener toolbarListener = new Listener() {
      public void handleEvent(Event event) {
        ToolItem toolItem = (ToolItem) event.widget;
        String caption = toolItem.getText();
        text.setText("You clicked " + caption);
      }
    };
    toolItem1.addListener(SWT.Selection, toolbarListener);
    toolItem2.addListener(SWT.Selection, toolbarListener);
    toolItem3.addListener(SWT.Selection, toolbarListener);
    toolItem4.addListener(SWT.Selection, toolbarListener);
    toolItem5.addListener(SWT.Selection, toolbarListener);
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }
    display.dispose();
  }
}





Test ToolBar

/******************************************************************************
 * All Right Reserved. 
 * Copyright (c) 1998, 2004 Jackwind Li Guojie
 * 
 * Created on 2004-4-22 20:53:55 by JACK
 * $Id$
 * 
 *****************************************************************************/

import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.ActionContributionItem;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.action.ToolBarManager;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Decorations;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.ToolBar;
public class TestToolBar {
  Display display = new Display();
  Shell shell = new Shell(display);
  public TestToolBar() {
    MenuManager menuManager = new MenuManager();
    
    ToolBar toolBar = new ToolBar(shell, SWT.FLAT | SWT.RIGHT);
    final ToolBarManager manager = new ToolBarManager(toolBar);
    // Adds tool bar items using actions.
    final Action actionForward =
      new Action(
        "&Forward",
        ImageDescriptor.createFromFile(
          null,
          "icons/web/forward.gif")) {
      public void run() {
        System.out.println("FORWARD");
      }
    };
    actionForward.setAccelerator(SWT.CTRL + "F");

    Action actionHome =
      new Action(
        "&Home",
        ImageDescriptor.createFromFile(null, "icons/web/home.gif")) {
      public void run() {
        System.out.println("HOME");
      }
    };
    actionHome.setAccelerator(SWT.CTRL + "H");

    manager.add(actionForward);
    
    ActionContributionItem item = new ActionContributionItem(actionHome);
    item.setMode(ActionContributionItem.MODE_FORCE_TEXT);
    manager.add(item);
    manager.update(true);
    toolBar.pack();
    
    MenuManager fileMenuManager = new MenuManager("&File");
    fileMenuManager.add(actionForward);
    fileMenuManager.add(actionHome);
    menuManager.add(fileMenuManager);
    menuManager.updateAll(true);
    shell.setMenuBar(menuManager.createMenuBar((Decorations)shell));

    shell.pack();
    shell.open();
    //textUser.forceFocus();
    // Set up the event loop.
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        // If no more entries in event queue
        display.sleep();
      }
    }
    display.dispose();
  }
  private void init() {
  }
  public static void main(String[] args) {
    new TestToolBar();
  }
}





This class creates a complex toolbar

//Send questions, comments, bug reports, etc. to the authors:
//Rob Warner (rwarner@interspatial.ru)
//Robert Harris (rbrt_harris@yahoo.ru)
import java.io.*;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.*;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;
/**
 * This class creates a complex toolbar. It has two regular push buttons, two
 * "toggle" push buttons, two "radio" push buttons, and two dropdowns.
 */
public class ToolBarComplex {
  private static final String IMAGE_PATH = "images"
      + System.getProperty("file.separator");
  // Images to use on our tool items
  private Image circle, grayCircle;
  private Image square, graySquare;
  private Image star, grayStar;
  private Image triangle, grayTriangle;
  // Labels to display tool item statuses
  private Label checkOneStatus;
  private Label checkTwoStatus;
  private Label radioStatus;
  private Label dropdownOneStatus;
  private Label dropdownTwoStatus;
  /**
   * Runs the application
   */
  public void run() {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("Toolbar with Images");
    createImages(shell);
    createContents(shell);
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
    disposeImages();
    display.dispose();
  }
  /**
   * Creates the images
   * 
   * @param shell the parent shell
   */
  private void createImages(Shell shell) {
    try {
      circle = new Image(shell.getDisplay(), new FileInputStream(IMAGE_PATH
          + "circle.gif"));
      grayCircle = new Image(shell.getDisplay(), new FileInputStream(IMAGE_PATH
          + "grayCircle.gif"));
      square = new Image(shell.getDisplay(), new FileInputStream(IMAGE_PATH
          + "square.gif"));
      graySquare = new Image(shell.getDisplay(), new FileInputStream(IMAGE_PATH
          + "graySquare.gif"));
      star = new Image(shell.getDisplay(), new FileInputStream(IMAGE_PATH
          + "star.gif"));
      grayStar = new Image(shell.getDisplay(), new FileInputStream(IMAGE_PATH
          + "grayStar.gif"));
      triangle = new Image(shell.getDisplay(), new FileInputStream(IMAGE_PATH
          + "triangle.gif"));
      grayTriangle = new Image(shell.getDisplay(), new FileInputStream(IMAGE_PATH
          + "grayTriangle.gif"));
    } catch (IOException e) {
      // Images not found; handle gracefully
    }
  }
  /**
   * Disposes the images
   */
  private void disposeImages() {
    if (circle != null)
      circle.dispose();
    if (grayCircle != null)
      grayCircle.dispose();
    if (square != null)
      square.dispose();
    if (graySquare != null)
      graySquare.dispose();
    if (star != null)
      star.dispose();
    if (grayStar != null)
      grayStar.dispose();
    if (triangle != null)
      triangle.dispose();
    if (grayTriangle != null)
      grayTriangle.dispose();
  }
  /**
   * Creates the window contents
   * 
   * @param shell the parent shell
   */
  private void createContents(Shell shell) {
    shell.setLayout(new RowLayout(SWT.VERTICAL));
    createToolbar(shell);
    // Create the labels to display the statuses of
    // the "check" and "radio" buttons
    Composite composite = new Composite(shell, SWT.NONE);
    composite.setLayout(new GridLayout(2, true));
    new Label(composite, SWT.RIGHT).setText("Check One Status:");
    checkOneStatus = new Label(composite, SWT.LEFT);
    checkOneStatus.setText("Off");
    new Label(composite, SWT.RIGHT).setText("Check Two Status:");
    checkTwoStatus = new Label(composite, SWT.LEFT);
    checkTwoStatus.setText("Off");
    new Label(composite, SWT.RIGHT).setText("Radio Status:");
    radioStatus = new Label(composite, SWT.LEFT);
    radioStatus.setText("None");
  }
  /**
   * Creates the toolbar
   * 
   * @param shell the parent shell
   */
  private void createToolbar(final Shell shell) {
    ToolBar toolBar = new ToolBar(shell, SWT.HORIZONTAL);
    // Create push buttons
    ToolItem item = createToolItem(toolBar, SWT.PUSH, "Button One", circle, null,
        "This is button one");
    item.addSelectionListener(new SelectionAdapter() {
      public void widgetSelected(SelectionEvent event) {
        showMessage(shell, "Button One Pressed");
      }
    });
    item = createToolItem(toolBar, SWT.PUSH, "Button Two", square, null,
        "This is button two");
    item.addSelectionListener(new SelectionAdapter() {
      public void widgetSelected(SelectionEvent event) {
        showMessage(shell, "Button Two Pressed");
      }
    });
    ToolItem myItem = new ToolItem(toolBar, SWT.SEPARATOR);
    // Create "check" buttons
    item = createToolItem(toolBar, SWT.CHECK, "Check One", grayStar, star,
        "This is check one");
    item.addSelectionListener(new SelectionAdapter() {
      public void widgetSelected(SelectionEvent event) {
        ToolItem item = (ToolItem) event.widget;
        checkOneStatus.setText(item.getSelection() ? "On" : "Off");
      }
    });
    item = createToolItem(toolBar, SWT.CHECK, "Check Two", grayTriangle,
        triangle, "This is check two");
    item.addSelectionListener(new SelectionAdapter() {
      public void widgetSelected(SelectionEvent event) {
        ToolItem item = (ToolItem) event.widget;
        checkTwoStatus.setText(item.getSelection() ? "On" : "Off");
      }
    });
    new ToolItem(toolBar, SWT.SEPARATOR);
    // Create "radio" buttons
    item = createToolItem(toolBar, SWT.RADIO, "Radio One", grayCircle, circle,
        "This is radio one");
    item.addSelectionListener(new SelectionAdapter() {
      public void widgetSelected(SelectionEvent event) {
        radioStatus.setText("One");
      }
    });
    item = createToolItem(toolBar, SWT.RADIO, "Radio Two", graySquare, square,
        "This is radio two");
    item.addSelectionListener(new SelectionAdapter() {
      public void widgetSelected(SelectionEvent event) {
        radioStatus.setText("Two");
      }
    });
    new ToolItem(toolBar, SWT.SEPARATOR);
    // Create dropdowns
    item = createToolItem(toolBar, SWT.DROP_DOWN, "Dropdown One", star, null,
        "This is dropdown one");
    DropdownSelectionListener listenerOne = new DropdownSelectionListener(item);
    listenerOne.add("Option One for One");
    listenerOne.add("Option Two for One");
    listenerOne.add("Option Three for One");
    item.addSelectionListener(listenerOne);
    item = createToolItem(toolBar, SWT.DROP_DOWN, "Dropdown Two", triangle, null,
        "This is dropdown two");
    DropdownSelectionListener listenerTwo = new DropdownSelectionListener(item);
    listenerTwo.add("Option One for Two");
    listenerTwo.add("Option Two for Two");
    listenerTwo.add("Option Three for Two");
    item.addSelectionListener(listenerTwo);
  }
  /**
   * Helper function to create tool item
   * 
   * @param parent the parent toolbar
   * @param type the type of tool item to create
   * @param text the text to display on the tool item
   * @param image the image to display on the tool item
   * @param hotImage the hot image to display on the tool item
   * @param toolTipText the tool tip text for the tool item
   * @return ToolItem
   */
  private ToolItem createToolItem(ToolBar parent, int type, String text,
      Image image, Image hotImage, String toolTipText) {
    ToolItem item = new ToolItem(parent, type);
    item.setText(text);
    item.setImage(image);
    item.setHotImage(hotImage);
    item.setToolTipText(toolTipText);
    return item;
  }
  /**
   * Helper method to display a message box. We use it to display a message when
   * a "push" button or "dropdown" button is pushed.
   * 
   * @param shell the parent shell for the message box
   * @param message the message to display
   */
  public static void showMessage(Shell shell, String message) {
    MessageBox msgBox = new MessageBox(shell, SWT.OK);
    msgBox.setMessage(message);
    msgBox.open();
  }
  /**
   * The application entry point
   * 
   * @param args the command line arguments
   */
  public static void main(String[] args) {
    new ToolBarComplex().run();
  }
}
/**
 * This class provides the "drop down" functionality for our dropdown tool items.
 */
class DropdownSelectionListener extends SelectionAdapter {
  private ToolItem dropdown;
  private Menu menu;
  /**
   * Constructs a DropdownSelectionListener
   * 
   * @param dropdown the dropdown this listener belongs to
   */
  public DropdownSelectionListener(ToolItem dropdown) {
    this.dropdown = dropdown;
    menu = new Menu(dropdown.getParent().getShell());
  }
  /**
   * Adds an item to the dropdown list
   * 
   * @param item the item to add
   */
  public void add(String item) {
    MenuItem menuItem = new MenuItem(menu, SWT.NONE);
    menuItem.setText(item);
    menuItem.addSelectionListener(new SelectionAdapter() {
      public void widgetSelected(SelectionEvent event) {
        MenuItem selected = (MenuItem) event.widget;
        dropdown.setText(selected.getText());
      }
    });
  }
  /**
   * Called when either the button itself or the dropdown arrow is clicked
   * 
   * @param event the event that trigged this call
   */
  public void widgetSelected(SelectionEvent event) {
    // If they clicked the arrow, we show the list
    if (event.detail == SWT.ARROW) {
      // Determine where to put the dropdown list
      ToolItem item = (ToolItem) event.widget;
      Rectangle rect = item.getBounds();
      Point pt = item.getParent().toDisplay(new Point(rect.x, rect.y));
      menu.setLocation(pt.x, pt.y + rect.height);
      menu.setVisible(true);
    } else {
      // They pushed the button; take appropriate action
      ToolBarComplex.showMessage(dropdown.getParent().getShell(), dropdown
          .getText()
          + " Pressed");
    }
  }
}





ToolBar Examples

/******************************************************************************
 * All Right Reserved. 
 * Copyright (c) 1998, 2004 Jackwind Li Guojie
 * 
 * Created on Feb 25, 2004 7:48:37 PM by JACK
 * $Id$
 * 
 *****************************************************************************/

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;
public class ToolBarExamples {
  Display display = new Display();
  Shell shell = new Shell(display);
  ToolBar toolBar;
  
  public ToolBarExamples() {
    toolBar = new ToolBar(shell, SWT.FLAT | SWT.WRAP | SWT.RIGHT);
    
    ToolItem itemPush = new ToolItem(toolBar, SWT.PUSH);
    itemPush.setText("PUSH item");
    Image icon = new Image(shell.getDisplay(), "icons/new.gif");
    itemPush.setImage(icon);
    
    ToolItem itemCheck = new ToolItem(toolBar, SWT.CHECK);
    itemCheck.setText("CHECK item");
    
    ToolItem itemRadio1 = new ToolItem(toolBar, SWT.RADIO);
    itemRadio1.setText("RADIO item 1");
    
    ToolItem itemRadio2 = new ToolItem(toolBar, SWT.RADIO);
    itemRadio2.setText("RADIO item 2");
    
    ToolItem itemSeparator = new ToolItem(toolBar, SWT.SEPARATOR);
    Text text = new Text(toolBar, SWT.BORDER | SWT.SINGLE);
    text.pack();
    itemSeparator.setWidth(text.getBounds().width);
    itemSeparator.setControl(text);
    
    final ToolItem itemDropDown = new ToolItem(toolBar, SWT.DROP_DOWN);
    itemDropDown.setText("DROP_DOWN item");
    itemDropDown.setToolTipText("Click here to see a drop down menu ...");
    
    final Menu menu = new Menu(shell, SWT.POP_UP);
    new MenuItem(menu, SWT.PUSH).setText("Menu item 1");
    new MenuItem(menu, SWT.PUSH).setText("Menu item 2");
    new MenuItem(menu, SWT.SEPARATOR);
    new MenuItem(menu, SWT.PUSH).setText("Menu item 3");
    
    itemDropDown.addListener(SWT.Selection, new Listener() {
      public void handleEvent(Event event) {
        if(event.detail == SWT.ARROW) {
          Rectangle bounds = itemDropDown.getBounds();
          Point point = toolBar.toDisplay(bounds.x, bounds.y + bounds.height);
          menu.setLocation(point);
          menu.setVisible(true);
        }
      }
    });
    
    Listener selectionListener = new Listener() {
      public void handleEvent(Event event) {
        ToolItem item = (ToolItem)event.widget;
        System.out.println(item.getText() + " is selected");
        if( (item.getStyle() & SWT.RADIO) != 0 || (item.getStyle() & SWT.CHECK) != 0 ) 
          System.out.println("Selection status: " + item.getSelection());
      }
    };
    
    itemPush.addListener(SWT.Selection, selectionListener);
    itemCheck.addListener(SWT.Selection, selectionListener);
    itemRadio1.addListener(SWT.Selection, selectionListener);
    itemRadio2.addListener(SWT.Selection, selectionListener);
    itemDropDown.addListener(SWT.Selection, selectionListener);
    toolBar.pack();
    
    shell.addListener(SWT.Resize, new Listener() {
      public void handleEvent(Event event) {
        Rectangle clientArea = shell.getClientArea();
        toolBar.setSize(toolBar.ruputeSize(clientArea.width, SWT.DEFAULT));
      }
    });
    
    shell.setSize(500, 100);
    shell.open();
    //textUser.forceFocus();
    // Set up the event loop.
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        // If no more entries in event queue
        display.sleep();
      }
    }
    display.dispose();
  }
  private void init() {
  }
  public static void main(String[] args) {
    new ToolBarExamples();
  }
}





Toolbar Shell Example

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.*;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.*;
public class ToolbarShellExample {    
        Display d;
        Shell s;
        public static void main(String[] a){
            new ToolbarShellExample();
        
        }

        ToolbarShellExample()    {
            d = new Display();
            s = new Shell(d);
            s.setSize(300,300);
            s.setText("A Shell Toolbar Example");
            
            final ToolBar bar = new ToolBar(s,SWT.HORIZONTAL);
            bar.setSize(300,70);
            bar.setLocation(0,0);
            // create images for toolbar buttons
            final Image saveIcon = new Image(d, "jexp.gif");
            final Image openIcon = new Image(d, "jexp.gif");
            final Image cutIcon = new Image(d, "jexp.gif");
            final Image copyIcon = new Image(d, "jexp.gif");
            final Image pasteIcon = new Image(d, "jexp.gif");
            // create and add the button for performing an open operation
            final ToolItem openToolItem = new ToolItem(bar, SWT.PUSH);
            openToolItem.setImage(openIcon);
            openToolItem.setText("Open");
            openToolItem.setToolTipText("Open File");
            
            //create and add the button for performing a save operation
            final ToolItem saveToolItem = new ToolItem(bar, SWT.PUSH);
            saveToolItem.setImage(saveIcon);
            saveToolItem.setText("Save");
            saveToolItem.setToolTipText("Save File");
            
           //create and add the button for performing a cut operation
            final ToolItem cutToolItem = new ToolItem(bar, SWT.PUSH);
            cutToolItem.setImage(cutIcon);
            cutToolItem.setText("Cut");
            cutToolItem.setToolTipText("Cut");
            
            // create and add the button for performing a copy operation
            final ToolItem copyToolItem = new ToolItem(bar, SWT.PUSH);
            copyToolItem.setImage(copyIcon);
            copyToolItem.setText("Copy");
            copyToolItem.setToolTipText("Copy");
            
            // create and add the button for performing a paste operation
            final ToolItem pasteToolItem = new ToolItem(bar, SWT.PUSH);
            pasteToolItem.setImage(pasteIcon);
            pasteToolItem.setText("Paste");
            pasteToolItem.setToolTipText("Paste");
            
            // create the menu
            Menu m = new Menu(s,SWT.BAR);
            // create a file menu and add an exit item
            final MenuItem file = new MenuItem(m, SWT.CASCADE);
            file.setText("&File");
            final Menu filemenu = new Menu(s, SWT.DROP_DOWN);
            file.setMenu(filemenu);
            final MenuItem openMenuItem = new MenuItem(filemenu, SWT.PUSH);
            openMenuItem.setText("&Open\tCTRL+O");
            openMenuItem.setAccelerator(SWT.CTRL+"O");
            final MenuItem saveMenuItem = new MenuItem(filemenu, SWT.PUSH);
            saveMenuItem.setText("&Save\tCTRL+S");
            saveMenuItem.setAccelerator(SWT.CTRL+"S");
            final MenuItem separator = new MenuItem(filemenu, SWT.SEPARATOR);
            final MenuItem exitMenuItem = new MenuItem(filemenu, SWT.PUSH);
            exitMenuItem.setText("E&xit");
            
            // create an edit menu and add cut copy and paste items
            final MenuItem edit = new MenuItem(m, SWT.CASCADE);
            edit.setText("&Edit");
            final Menu editmenu = new Menu(s, SWT.DROP_DOWN);
            edit.setMenu(editmenu);
            final MenuItem cutMenuItem = new MenuItem(editmenu, SWT.PUSH);
            cutMenuItem.setText("&Cut");
            final MenuItem copyMenuItem = new MenuItem(editmenu, SWT.PUSH);
            copyMenuItem.setText("Co&py");
            final MenuItem pasteMenuItem = new MenuItem(editmenu, SWT.PUSH);
            pasteMenuItem.setText("&Paste");            
          
            //create a Window menu and add Child item
            final MenuItem window = new MenuItem(m, SWT.CASCADE);
            window.setText("&Window");
            final Menu windowmenu = new Menu(s, SWT.DROP_DOWN);
            window.setMenu(windowmenu);
            final MenuItem maxMenuItem = new MenuItem(windowmenu, SWT.PUSH);
            maxMenuItem.setText("Ma&ximize");
            final MenuItem minMenuItem = new MenuItem(windowmenu, SWT.PUSH);
            minMenuItem.setText("Mi&nimize");
            
            // create a Help menu and add an about item
            final MenuItem help = new MenuItem(m, SWT.CASCADE);
            help.setText("&Help");
            final Menu helpmenu = new Menu(s, SWT.DROP_DOWN);
            help.setMenu(helpmenu);
            final MenuItem aboutMenuItem = new MenuItem(helpmenu, SWT.PUSH);
            aboutMenuItem.setText("&About");    
            
            // add action listeners for the menu items
            
            openMenuItem.addSelectionListener(new SelectionListener() {
                public void widgetSelected(SelectionEvent e) {
                    System.out.println("Open");
                }
                public void widgetDefaultSelected(SelectionEvent e) {                
               }
               });
         
           saveMenuItem.addSelectionListener(new SelectionListener() {
                 public void widgetSelected(SelectionEvent e) {
                     System.out.println("Save");
                }
                 public void widgetDefaultSelected(SelectionEvent e) {                
                }
                });
           exitMenuItem.addSelectionListener(new SelectionListener() {
                public void widgetSelected(SelectionEvent e) {
                    System.exit(0);
                }
                public void widgetDefaultSelected(SelectionEvent e) {                
                }
                });
        
           cutMenuItem.addSelectionListener(new SelectionListener() {
                public void widgetSelected(SelectionEvent e) {
                    System.out.println("Cut");
                }
                public void widgetDefaultSelected(SelectionEvent e) {                
                }
                });
        
           copyMenuItem.addSelectionListener(new SelectionListener() {
                public void widgetSelected(SelectionEvent e) {
                    System.out.println("Copy");
                }
                public void widgetDefaultSelected(SelectionEvent e) {               
                }
                });
        
           pasteMenuItem.addSelectionListener(new SelectionListener() {
                public void widgetSelected(SelectionEvent e) {
                    System.out.println("Paste");
                }
                public void widgetDefaultSelected(SelectionEvent e) {                
                }
                });
        
           maxMenuItem.addSelectionListener(new SelectionListener() {
                public void widgetSelected(SelectionEvent e) {
                    Shell parent = (Shell)maxMenuItem.getParent().getParent();
                    parent.setMaximized(true);
                }
                public void widgetDefaultSelected(SelectionEvent e) {                
                }
                });
        
           minMenuItem.addSelectionListener(new SelectionListener() {
                public void widgetSelected(SelectionEvent e) {
                    Shell parent = (Shell)minMenuItem.getParent().getParent();
                    parent.setMaximized(false);
                }
                public void widgetDefaultSelected(SelectionEvent e) {                
                }
                });
           aboutMenuItem.addSelectionListener(new SelectionListener() {
                public void widgetSelected(SelectionEvent e) {
                    System.out.println("Help Invoked");
              }
                public void widgetDefaultSelected(SelectionEvent e) {                
                }
                });            
            
            s.setMenuBar(m);
        
            s.open();
            while(!s.isDisposed()){
                if(!d.readAndDispatch())
                    d.sleep();
            }
            d.dispose();
        }        
}





Toolbar Shell Example 2

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;
public class ToolbarShellExample2 {
  Display d;
  Shell s;
  public static void main(String[] a){
      new ToolbarShellExample2();
  
  }
  ToolbarShellExample2() {
    d = new Display();
    s = new Shell(d);
    s.setSize(300, 300);
    s.setText("A Shell Toolbar Example");
    final ToolBar bar = new ToolBar(s, SWT.HORIZONTAL);
    bar.setSize(500, 70);
    bar.setLocation(0, 0);
    // create images for toolbar buttons
    final Image saveIcon = new Image(d, "jexp.gif");
    final Image openIcon = new Image(d, "jexp.gif");
    final Image childIcon = new Image(d, "jexp.gif");
    final Image cutIcon = new Image(d, "jexp.gif");
    final Image copyIcon = new Image(d, "jexp.gif");
    final Image pasteIcon = new Image(d, "jexp.gif");
    // create and add the button for performing an open operation
    final ToolItem openToolItem = new ToolItem(bar, SWT.PUSH);
    openToolItem.setImage(openIcon);
    openToolItem.setText("Open");
    openToolItem.setToolTipText("Open File");
    //create and add the button for performing a save operation
    final ToolItem saveToolItem = new ToolItem(bar, SWT.PUSH);
    saveToolItem.setImage(saveIcon);
    saveToolItem.setText("Save");
    saveToolItem.setToolTipText("Save File");
    final ToolItem sep1 = new ToolItem(bar, SWT.SEPARATOR);
    //create and add the button for performing a cut operation
    final ToolItem cutToolItem = new ToolItem(bar, SWT.PUSH);
    cutToolItem.setImage(cutIcon);
    cutToolItem.setText("Cut");
    cutToolItem.setToolTipText("Cut");
    // create and add the button for performing a copy operation
    final ToolItem copyToolItem = new ToolItem(bar, SWT.PUSH);
    copyToolItem.setImage(copyIcon);
    copyToolItem.setText("Copy");
    copyToolItem.setToolTipText("Copy");
    // create and add the button for performing a paste operation
    final ToolItem pasteToolItem = new ToolItem(bar, SWT.PUSH);
    pasteToolItem.setImage(pasteIcon);
    pasteToolItem.setText("Paste");
    pasteToolItem.setToolTipText("Paste");
    // create inner classes for SelectionListeners
    class Open implements SelectionListener {
      public void widgetSelected(SelectionEvent event) {
        System.out.println("Open");
      }
      public void widgetDefaultSelected(SelectionEvent event) {
      }
    }
    class Save implements SelectionListener {
      public void widgetSelected(SelectionEvent event) {
        System.out.println("Save");
      }
      public void widgetDefaultSelected(SelectionEvent event) {
      }
    }
    class Cut implements SelectionListener {
      public void widgetSelected(SelectionEvent event) {
        System.out.println("Cut");
      }
      public void widgetDefaultSelected(SelectionEvent event) {
      }
    }
    class Copy implements SelectionListener {
      public void widgetSelected(SelectionEvent event) {
        System.out.println("Copy");
      }
      public void widgetDefaultSelected(SelectionEvent event) {
      }
    }
    class Paste implements SelectionListener {
      public void widgetSelected(SelectionEvent event) {
        System.out.println("Paste");
      }
      public void widgetDefaultSelected(SelectionEvent event) {
      }
    }
    openToolItem.addSelectionListener(new Open());
    saveToolItem.addSelectionListener(new Save());
    cutToolItem.addSelectionListener(new Cut());
    copyToolItem.addSelectionListener(new Copy());
    pasteToolItem.addSelectionListener(new Paste());
    // create the menu system
    Menu m = new Menu(s, SWT.BAR);
    // create a file menu and add an exit item
    final MenuItem file = new MenuItem(m, SWT.CASCADE);
    file.setText("&File");
    final Menu filemenu = new Menu(s, SWT.DROP_DOWN);
    file.setMenu(filemenu);
    final MenuItem openMenuItem = new MenuItem(filemenu, SWT.PUSH);
    openMenuItem.setText("&Open\tCTRL+O");
    openMenuItem.setAccelerator(SWT.CTRL + "O");
    final MenuItem saveMenuItem = new MenuItem(filemenu, SWT.PUSH);
    saveMenuItem.setText("&Save\tCTRL+S");
    saveMenuItem.setAccelerator(SWT.CTRL + "S");
    final MenuItem separator = new MenuItem(filemenu, SWT.SEPARATOR);
    final MenuItem exitMenuItem = new MenuItem(filemenu, SWT.PUSH);
    exitMenuItem.setText("E&xit");
    // create an edit menu and add cut copy and paste items
    final MenuItem edit = new MenuItem(m, SWT.CASCADE);
    edit.setText("&Edit");
    final Menu editmenu = new Menu(s, SWT.DROP_DOWN);
    edit.setMenu(editmenu);
    final MenuItem cutMenuItem = new MenuItem(editmenu, SWT.PUSH);
    cutMenuItem.setText("&Cut");
    final MenuItem copyMenuItem = new MenuItem(editmenu, SWT.PUSH);
    copyMenuItem.setText("Co&py");
    final MenuItem pasteMenuItem = new MenuItem(editmenu, SWT.PUSH);
    pasteMenuItem.setText("&Paste");
    //create a Window menu and add Child item
    final MenuItem window = new MenuItem(m, SWT.CASCADE);
    window.setText("&Window");
    final Menu windowmenu = new Menu(s, SWT.DROP_DOWN);
    window.setMenu(windowmenu);
    final MenuItem maxMenuItem = new MenuItem(windowmenu, SWT.PUSH);
    maxMenuItem.setText("Ma&ximize");
    final MenuItem minMenuItem = new MenuItem(windowmenu, SWT.PUSH);
    minMenuItem.setText("Mi&nimize");
    // create a Help menu and add an about item
    final MenuItem help = new MenuItem(m, SWT.CASCADE);
    help.setText("&Help");
    final Menu helpmenu = new Menu(s, SWT.DROP_DOWN);
    help.setMenu(helpmenu);
    final MenuItem aboutMenuItem = new MenuItem(helpmenu, SWT.PUSH);
    aboutMenuItem.setText("&About");
    // add action listeners for the menu items
    openMenuItem.addSelectionListener(new Open());
    saveMenuItem.addSelectionListener(new Save());
    exitMenuItem.addSelectionListener(new SelectionListener() {
      public void widgetSelected(SelectionEvent e) {
        System.exit(0);
      }
      public void widgetDefaultSelected(SelectionEvent e) {
      }
    });
    cutMenuItem.addSelectionListener(new Cut());
    copyMenuItem.addSelectionListener(new Copy());
    pasteMenuItem.addSelectionListener(new Paste());
    maxMenuItem.addSelectionListener(new SelectionListener() {
      public void widgetSelected(SelectionEvent e) {
        Shell parent = (Shell) maxMenuItem.getParent().getParent();
        parent.setMaximized(true);
      }
      public void widgetDefaultSelected(SelectionEvent e) {
      }
    });
    minMenuItem.addSelectionListener(new SelectionListener() {
      public void widgetSelected(SelectionEvent e) {
        Shell parent = (Shell) minMenuItem.getParent().getParent();
        parent.setMaximized(false);
      }
      public void widgetDefaultSelected(SelectionEvent e) {
      }
    });
    aboutMenuItem.addSelectionListener(new SelectionListener() {
      public void widgetSelected(SelectionEvent e) {
        System.out.println("Help Invoked");
      }
      public void widgetDefaultSelected(SelectionEvent e) {
      }
    });
    s.setMenuBar(m);
    s.open();
    while (!s.isDisposed()) {
      if (!d.readAndDispatch())
        d.sleep();
    }
    d.dispose();
  }
}