Java/SWT JFace Eclipse/CoolBar

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

Control example snippet: print key state, code and character

/*
 * Control example snippet: print key state, code and character
 *
 * 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.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
public class Snippet25 {
  static String stateMask(int stateMask) {
    String string = "";
    if ((stateMask & SWT.CTRL) != 0)
      string += " CTRL";
    if ((stateMask & SWT.ALT) != 0)
      string += " ALT";
    if ((stateMask & SWT.SHIFT) != 0)
      string += " SHIFT";
    if ((stateMask & SWT.ruMAND) != 0)
      string += " COMMAND";
    return string;
  }
  static String character(char character) {
    switch (character) {
    case 0:
      return ""\\0"";
    case SWT.BS:
      return ""\\b"";
    case SWT.CR:
      return ""\\r"";
    case SWT.DEL:
      return "DEL";
    case SWT.ESC:
      return "ESC";
    case SWT.LF:
      return ""\\n"";
    case SWT.TAB:
      return ""\\t"";
    }
    return """ + character + """;
  }
  static String keyCode(int keyCode) {
    switch (keyCode) {
    /* Keyboard and Mouse Masks */
    case SWT.ALT:
      return "ALT";
    case SWT.SHIFT:
      return "SHIFT";
    case SWT.CONTROL:
      return "CONTROL";
    case SWT.ruMAND:
      return "COMMAND";
    /* Non-Numeric Keypad Keys */
    case SWT.ARROW_UP:
      return "ARROW_UP";
    case SWT.ARROW_DOWN:
      return "ARROW_DOWN";
    case SWT.ARROW_LEFT:
      return "ARROW_LEFT";
    case SWT.ARROW_RIGHT:
      return "ARROW_RIGHT";
    case SWT.PAGE_UP:
      return "PAGE_UP";
    case SWT.PAGE_DOWN:
      return "PAGE_DOWN";
    case SWT.HOME:
      return "HOME";
    case SWT.END:
      return "END";
    case SWT.INSERT:
      return "INSERT";
    /* Virtual and Ascii Keys */
    case SWT.BS:
      return "BS";
    case SWT.CR:
      return "CR";
    case SWT.DEL:
      return "DEL";
    case SWT.ESC:
      return "ESC";
    case SWT.LF:
      return "LF";
    case SWT.TAB:
      return "TAB";
    /* Functions Keys */
    case SWT.F1:
      return "F1";
    case SWT.F2:
      return "F2";
    case SWT.F3:
      return "F3";
    case SWT.F4:
      return "F4";
    case SWT.F5:
      return "F5";
    case SWT.F6:
      return "F6";
    case SWT.F7:
      return "F7";
    case SWT.F8:
      return "F8";
    case SWT.F9:
      return "F9";
    case SWT.F10:
      return "F10";
    case SWT.F11:
      return "F11";
    case SWT.F12:
      return "F12";
    case SWT.F13:
      return "F13";
    case SWT.F14:
      return "F14";
    case SWT.F15:
      return "F15";
    /* Numeric Keypad Keys */
    case SWT.KEYPAD_ADD:
      return "KEYPAD_ADD";
    case SWT.KEYPAD_SUBTRACT:
      return "KEYPAD_SUBTRACT";
    case SWT.KEYPAD_MULTIPLY:
      return "KEYPAD_MULTIPLY";
    case SWT.KEYPAD_DIVIDE:
      return "KEYPAD_DIVIDE";
    case SWT.KEYPAD_DECIMAL:
      return "KEYPAD_DECIMAL";
    case SWT.KEYPAD_CR:
      return "KEYPAD_CR";
    case SWT.KEYPAD_0:
      return "KEYPAD_0";
    case SWT.KEYPAD_1:
      return "KEYPAD_1";
    case SWT.KEYPAD_2:
      return "KEYPAD_2";
    case SWT.KEYPAD_3:
      return "KEYPAD_3";
    case SWT.KEYPAD_4:
      return "KEYPAD_4";
    case SWT.KEYPAD_5:
      return "KEYPAD_5";
    case SWT.KEYPAD_6:
      return "KEYPAD_6";
    case SWT.KEYPAD_7:
      return "KEYPAD_7";
    case SWT.KEYPAD_8:
      return "KEYPAD_8";
    case SWT.KEYPAD_9:
      return "KEYPAD_9";
    case SWT.KEYPAD_EQUAL:
      return "KEYPAD_EQUAL";
    /* Other keys */
    case SWT.CAPS_LOCK:
      return "CAPS_LOCK";
    case SWT.NUM_LOCK:
      return "NUM_LOCK";
    case SWT.SCROLL_LOCK:
      return "SCROLL_LOCK";
    case SWT.PAUSE:
      return "PAUSE";
    case SWT.BREAK:
      return "BREAK";
    case SWT.PRINT_SCREEN:
      return "PRINT_SCREEN";
    case SWT.HELP:
      return "HELP";
    }
    return character((char) keyCode);
  }
  public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    Listener listener = new Listener() {
      public void handleEvent(Event e) {
        String string = e.type == SWT.KeyDown ? "DOWN:" : "UP  :";
        string += " stateMask=0x" + Integer.toHexString(e.stateMask)
            + stateMask(e.stateMask) + ",";
        string += " keyCode=0x" + Integer.toHexString(e.keyCode) + " "
            + keyCode(e.keyCode) + ",";
        string += " character=0x" + Integer.toHexString(e.character)
            + " " + character(e.character);
        System.out.println(string);
      }
    };
    shell.addListener(SWT.KeyDown, listener);
    shell.addListener(SWT.KeyUp, listener);
    shell.setSize(200, 200);
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }
    display.dispose();
  }
}





Control example snippet: print mouse state and button (down, move, up)

/*
 * Control example snippet: print mouse state and button (down, move, up)
 *
 * 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.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
public class Snippet62 {
  public static void main(String[] args) {
    Display display = new Display();
    final Shell shell = new Shell(display);
    Listener listener = new Listener() {
      public void handleEvent(Event e) {
        String string = "Unknown";
        switch (e.type) {
        case SWT.MouseDown:
          string = "DOWN";
          break;
        case SWT.MouseMove:
          string = "MOVE";
          break;
        case SWT.MouseUp:
          string = "UP";
          break;
        }
        string += ": button: " + e.button + ", ";
        string += "stateMask=0x" + Integer.toHexString(e.stateMask);
        if ((e.stateMask & SWT.BUTTON1) != 0)
          string += " BUTTON1";
        if ((e.stateMask & SWT.BUTTON2) != 0)
          string += " BUTTON2";
        if ((e.stateMask & SWT.BUTTON3) != 0)
          string += " BUTTON3";
        if ((e.stateMask & SWT.BUTTON4) != 0)
          string += " BUTTON4";
        if ((e.stateMask & SWT.BUTTON5) != 0)
          string += " BUTTON5";
        System.out.println(string);
      }
    };
    shell.addListener(SWT.MouseDown, listener);
    shell.addListener(SWT.MouseMove, listener);
    shell.addListener(SWT.MouseUp, listener);
    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }
    display.dispose();
  }
}





Coolbar Example

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.CoolBar;
import org.eclipse.swt.widgets.CoolItem;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;
public class CoolbarShellExample {
  Display d;
  Shell s;
  CoolbarShellExample() {
    d = new Display();
    s = new Shell(d);
    s.setSize(300, 300);
    
    s.setText("A Shell Coolbar Example");
    final CoolBar bar = new CoolBar(s, SWT.BORDER);
    bar.setSize(280, 70);
    bar.setLocation(0, 0);
    // create images for coolbar buttons
    final Image saveIcon = new Image(d, "save.jpg");
    final Image openIcon = new Image(d, "open.jpg");
    final Image cutIcon = new Image(d, "cut.jpg");
    final Image copyIcon = new Image(d, "copy.jpg");
    final Image pasteIcon = new Image(d, "paste.jpg");
    // create and add the button for performing an open operation
    final CoolItem openCoolItem = new CoolItem(bar, SWT.NONE);
    final Button openBtn = new Button(bar, SWT.PUSH);
    openBtn.setImage(openIcon);
    openBtn.pack();
    Point size = openBtn.getSize();
    openCoolItem.setControl(openBtn);
    openCoolItem.setSize(openCoolItem.ruputeSize(size.x, size.y));
    //create and add the button for performing a save operation
    final CoolItem saveCoolItem = new CoolItem(bar, SWT.PUSH);
    final Button saveBtn = new Button(bar, SWT.PUSH);
    saveBtn.setImage(saveIcon);
    saveBtn.pack();
    size = saveBtn.getSize();
    saveCoolItem.setControl(saveBtn);
    saveCoolItem.setSize(saveCoolItem.ruputeSize(size.x, size.y));
    //create and add the button for performing a cut operation
    final CoolItem cutCoolItem = new CoolItem(bar, SWT.PUSH);
    final Button cutBtn = new Button(bar, SWT.PUSH);
    cutBtn.setImage(cutIcon);
    cutBtn.pack();
    size = cutBtn.getSize();
    cutCoolItem.setControl(cutBtn);
    cutCoolItem.setSize(cutCoolItem.ruputeSize(size.x, size.y));
    // create and add the button for performing a copy operation
    final CoolItem copyCoolItem = new CoolItem(bar, SWT.PUSH);
    final Button copyBtn = new Button(bar, SWT.PUSH);
    copyBtn.setImage(copyIcon);
    copyBtn.pack();
    size = copyBtn.getSize();
    copyCoolItem.setControl(copyBtn);
    copyCoolItem.setSize(copyCoolItem.ruputeSize(size.x, size.y));
    // create and add the button for performing a paste operation
    final CoolItem pasteCoolItem = new CoolItem(bar, SWT.PUSH);
    final Button pasteBtn = new Button(bar, SWT.PUSH);
    pasteBtn.setImage(pasteIcon);
    pasteBtn.pack();
    size = pasteBtn.getSize();
    pasteCoolItem.setControl(pasteBtn);
    pasteCoolItem.setSize(pasteCoolItem.ruputeSize(size.x, size.y));
    pasteCoolItem.setMinimumSize(size);
    openBtn.addSelectionListener(new SelectionAdapter() {
      public void widgetSelected(SelectionEvent event) {
        System.out.println("Open");
      }
    });
    saveBtn.addSelectionListener(new SelectionAdapter() {
      public void widgetSelected(SelectionEvent event) {
        System.out.println("Save");
      }
    });
    cutBtn.addSelectionListener(new SelectionAdapter() {
      public void widgetSelected(SelectionEvent event) {
        System.out.println("Cut");
      }
    });
    copyBtn.addSelectionListener(new SelectionAdapter() {
      public void widgetSelected(SelectionEvent event) {
        System.out.println("Copy");
      }
    });
    pasteBtn.addSelectionListener(new SelectionAdapter() {
      public void widgetSelected(SelectionEvent event) {
        System.out.println("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();
  }
  public static void main() {
    new CoolbarShellExample();
  }
}





Coolbar 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.graphics.Point;
import org.eclipse.swt.widgets.rubo;
import org.eclipse.swt.widgets.CoolBar;
import org.eclipse.swt.widgets.CoolItem;
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 CoolbarShellExample2 {
    Display d;
    Shell s;
    
    CoolbarShellExample2()    {
        d = new Display();
        s = new Shell(d);
        s.setSize(400,300);
        
        s.setText("A Shell Coolbar Example");
        
        final CoolBar coolBar = new CoolBar(s,SWT.BORDER);
        coolBar.setSize(395,70);
        coolBar.setLocation(0,0);
        // create images for toolbar buttons
        final Image saveIcon = new Image(d, "c:\\icons\\save.jpg");
        final Image openIcon = new Image(d, "c:\\icons\\open.jpg");
        final Image childIcon = new Image(d, "c:\\icons\\userH.ico");
        final Image cutIcon = new Image(d, "c:\\icons\\cut.jpg");
        final Image copyIcon = new Image(d, "c:\\icons\\copy.jpg");
        final Image pasteIcon = new Image(d, "c:\\icons\\paste.jpg");
        
        // create and add the button for performing an open operation
        final CoolItem openCoolItem = new CoolItem(coolBar, SWT.NONE);
       
        final ToolBar fileToolBar = new ToolBar(coolBar,SWT.HORIZONTAL);
        final ToolItem openToolItem = new ToolItem(fileToolBar, SWT.PUSH);
        openToolItem.setImage(openIcon);
        openToolItem.setText("Open");
        openToolItem.setToolTipText("Open");
        
        final ToolItem saveToolItem = new ToolItem(fileToolBar, SWT.PUSH);
        saveToolItem.setImage(openIcon);
        saveToolItem.setText("Save");
        saveToolItem.setToolTipText("Save");
        
        fileToolBar.pack(); 
        Point size = fileToolBar.getSize();
        openCoolItem.setControl(fileToolBar);
        openCoolItem.setSize(openCoolItem.ruputeSize(size.x, size.y));
        
        final CoolItem editbarCoolItem = new CoolItem(coolBar, SWT.PUSH);
        final ToolBar editToolBar = new ToolBar(coolBar,SWT.HORIZONTAL);
        
//        create and add the button for performing a cut operation
        final ToolItem cutToolItem = new ToolItem(editToolBar, 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(editToolBar, 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(editToolBar, SWT.PUSH);
        pasteToolItem.setImage(pasteIcon);
        pasteToolItem.setText("Paste");
        pasteToolItem.setToolTipText("Paste");
        editToolBar.pack();
        size = editToolBar.getSize();
        editbarCoolItem.setControl(editToolBar);
        editbarCoolItem.setSize(editbarCoolItem.ruputeSize(size.x, size.y));
        
        final CoolItem fontCoolItem = new CoolItem(coolBar, SWT.PUSH);
        final Combo fontCombo = new Combo(coolBar, SWT.READ_ONLY | SWT.BORDER);
        String[] items = {"Arial", "Courier", "Times New Roman"};
        fontCombo.setItems(items);
        fontCombo.pack();
        size = fontCombo.getSize();
        fontCoolItem.setControl(fontCombo);
        fontCoolItem.setSize(fontCoolItem.ruputeSize(size.x, size.y));
        fontCoolItem.setMinimumSize(size);
        
              
        openToolItem.addListener(SWT.Selection, new Listener() {
            public void handleEvent(Event event) {
                System.out.println("Open");
                
            }
        });
        
        saveToolItem.addListener(SWT.Selection, new Listener() {
            public void handleEvent(Event event) {
                System.out.println("Save");
                
            }
        });
        
        cutToolItem.addListener(SWT.Selection, new Listener() {
                public void handleEvent(Event event) {
                    System.out.println("Cut");
                    
                }
            });
            
            copyToolItem.addListener(SWT.Selection, new Listener() {
                public void handleEvent(Event event) {
                    System.out.println("Copy");
                    
                }
            });
            
            pasteToolItem.addListener(SWT.Selection, new Listener() {
                public void handleEvent(Event event) {
                    System.out.println("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();
    }
    public static void main(){
      new CoolbarShellExample2();
    }
}





CoolBar Examples

/*******************************************************************************
 * All Right Reserved. Copyright (c) 1998, 2004 Jackwind Li Guojie
 * 
 * Created on Feb 27, 2004 12:17:28 AM by JACK $Id$
 *  
 ******************************************************************************/

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.ruposite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.CoolBar;
import org.eclipse.swt.widgets.CoolItem;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
public class CoolBarExamples {
  Display display = new Display();
  Shell shell = new Shell(display);
  public CoolBarExamples() {
    shell.setLayout(new GridLayout());
    final CoolBar coolBar = new CoolBar(shell, SWT.NONE);
    coolBar.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    // cool item with a text field.
    CoolItem textItem = new CoolItem(coolBar, SWT.NONE);
    Text text = new Text(coolBar, SWT.BORDER | SWT.DROP_DOWN);
    text.setText("TEXT");
    text.pack();
    Point size = text.getSize();
    textItem.setControl(text);
    textItem.setSize(textItem.ruputeSize(size.x, size.y));
    // cool item with a label.
    CoolItem labelItem = new CoolItem(coolBar, SWT.NONE);
    Label label = new Label(coolBar, SWT.NONE);
    label.setText("LABEL");
    label.pack();
    size = label.getSize();
    labelItem.setControl(label);
    labelItem.setSize(textItem.ruputeSize(size.x, size.y));
    // cool item with a button.
    CoolItem buttonItem = new CoolItem(coolBar, SWT.NONE | SWT.DROP_DOWN);
    Composite composite = new Composite(coolBar, SWT.NONE);
    composite.setLayout(new GridLayout(2, true));
    Button button1 = new Button(composite, SWT.PUSH);
    button1.setText("Button 1");
    button1.pack();
    Button button2 = new Button(composite, SWT.PUSH);
    button2.setText("Button 2");
    button2.pack();
    composite.pack();
    size = composite.getSize();
    buttonItem.setControl(composite);
    buttonItem.setSize(buttonItem.ruputeSize(size.x, size.y));
//    // Test cool item adding method.
//    Label label2 = new Label(coolBar, SWT.NONE);
//    label2.setText("label2");
//    addControlToCoolBar(label2, SWT.DROP_DOWN, coolBar);
    try {
      setState(coolBar, new File("coolbar.state"));
    } catch (IOException e1) {
      e1.printStackTrace();
    }
    
    shell.addListener(SWT.Close, new Listener() {
      public void handleEvent(Event event) {
        try {
          saveState(coolBar, new File("coolbar.state") );
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
    });
    shell.setSize(300, 120);
    // shell.pack();
    shell.open();
    // Set up the event loop.
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        // If no more entries in event queue
        display.sleep();
      }
    }
    display.dispose();
  }
  /**
   * Creates a cool item with the given control and add the cool item to the
   * specified cool bar.
   * 
   * @param control
   * @param coolItemStyle -
   *            should be SWT.NONE or SWT.DROP_DOWN.
   * @param coolBar
   * @return the cool item created.
   */
  public static CoolItem addControlToCoolBar(
    Control control,
    int coolItemStyle,
    CoolBar coolBar) {
    CoolItem coolItem = new CoolItem(coolBar, coolItemStyle);
    Point size = control.getSize();
    if (size.x == 0 && size.y == 0) {
      // The control size has not been set yet.
      // Pack the control and recalculate its size.
      control.pack();
      size = control.getSize();
    }
    coolItem.setControl(control);
    coolItem.setSize(coolItem.ruputeSize(size.x, size.y));
    return coolItem;
  }
  
  // Save the display state of the given cool bar in the specified file. 
  private void saveState(CoolBar coolBar, File file) throws IOException {
    DataOutputStream out = new DataOutputStream(new FileOutputStream(file));
    
    try {
      // Orders of items. 
      System.out.println("Item order: " + intArrayToString(coolBar.getItemOrder()));
      int[] order = coolBar.getItemOrder();
      out.writeInt(order.length);
      for(int i=0; i<order.length; i++)
        out.writeInt(order[i]);
      
      // Wrap indices.
      System.out.println("Wrap indices: " + intArrayToString(coolBar.getWrapIndices()));
      int[] wrapIndices = coolBar.getWrapIndices();
      out.writeInt(wrapIndices.length);
      for(int i=0; i<wrapIndices.length; i++)
        out.writeInt(wrapIndices[i]);
      
      // Sizes. 
      Point[] sizes = coolBar.getItemSizes();
      out.writeInt(sizes.length);
      for(int i=0; i<sizes.length; i++) {
        out.writeInt(sizes[i].x);
        out.writeInt(sizes[i].y);
      }
    } finally {
      out.close();
    }
    
  }
  
  // Sets the display state for a cool bar, using the saved information in the given file. 
  private void setState(CoolBar coolBar, File file) throws IOException {
    if(! file.exists())
      throw new IOException("File does not exist: " + file);
    
    DataInputStream in = new DataInputStream(new FileInputStream(file));
    
    try {
      // Order
      int size = in.readInt();
      int[] order = new int[size];
      for(int i=0; i<order.length; i++)
        order[i] = in.readInt();
      
      // Wrap indices.
      size = in.readInt();
      int[] wrapIndices = new int[size];
      for(int i=0; i<wrapIndices.length; i++)
        wrapIndices[i] = in.readInt();
      
      // Sizes.
      size = in.readInt();
      Point[] sizes = new Point[size];
      for(int i=0; i<sizes.length; i++) 
        sizes[i] = new Point(in.readInt(), in.readInt());
      
      coolBar.setItemLayout(order, wrapIndices, sizes);
    } finally {
      in.close();
    }
    
  }
  public static String intArrayToString(int values[]) {
    StringBuffer sb = new StringBuffer();
    sb.append("{");
    for (int i = 0; values != null && i < values.length; i++) {
      sb.append(values[i]);
      if (i != values.length - 1)
        sb.append(", ");
    }
    sb.append("}");
    return sb.toString();
  }
  public static void main(String[] args) {
    new CoolBarExamples();
  }
}





CoolBar example snippet: create a cool bar

/*
 * CoolBar example snippet: create a cool 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.widgets.Button;
import org.eclipse.swt.widgets.CoolBar;
import org.eclipse.swt.widgets.CoolItem;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class Snippet20 {
public static void main (String [] args) {
  Display display = new Display ();
  Shell shell = new Shell (display);
  CoolBar bar = new CoolBar (shell, SWT.BORDER);
  for (int i=0; i<2; i++) {
    CoolItem item = new CoolItem (bar, SWT.NONE);
    Button button = new Button (bar, SWT.PUSH);
    button.setText ("Button " + i);
    Point size = button.ruputeSize (SWT.DEFAULT, SWT.DEFAULT);
    item.setPreferredSize (item.ruputeSize (size.x, size.y));
    item.setControl (button);
  }
  bar.pack ();
  shell.open ();
  while (!shell.isDisposed ()) {
    if (!display.readAndDispatch ()) display.sleep ();
  }
  display.dispose ();
}
}





CoolBarTest

//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.events.*;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.SWT;
public class CoolBarTest {
  private static final String IMAGE_PATH = "images"
      + System.getProperty("file.separator");
  private Image circle;
  private Image square;
  private Image star;
  private Image triangle;
  /**
   * Runs the application
   */
  public void run() {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("CoolBar Test");
    createImages(shell);
    createContents(shell);
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
    disposeImages();
    display.dispose();
  }
  /**
   * Creates the window contents
   * 
   * @param shell the parent shell
   */
  private void createContents(Shell shell) {
    shell.setLayout(new GridLayout(1, false));
    CoolBar coolbar = createCoolBar(shell);
    coolbar.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
  }
  /**
   * Creates the CoolBar
   * 
   * @param shell the parent shell
   * @return CoolBar
   */
  private CoolBar createCoolBar(Shell shell) {
    CoolBar coolbar = new CoolBar(shell, SWT.NONE);
    // Create toolbar coolitem
    final CoolItem item = new CoolItem(coolbar, SWT.DROP_DOWN);
    item.setControl(createToolBar(coolbar));
    calcSize(item);
    // Add a listener to handle clicks on the chevron button
    item.addSelectionListener(new SelectionAdapter() {
      public void widgetSelected(SelectionEvent event) {
        calcSize(item);
      }
    });
    // Create combo coolitem
    CoolItem item2 = new CoolItem(coolbar, SWT.NONE);
    item2.setControl(createCombo(coolbar));
    calcSize(item2);
    // Create a dropdown coolitem
    item2 = new CoolItem(coolbar, SWT.NONE);
    item2.setControl(createStackedButtons(coolbar));
    calcSize(item2);
    return coolbar;
  }
  /**
   * Creates the ToolBar
   * 
   * @param composite the parent composite
   * @return Control
   */
  private Control createToolBar(Composite composite) {
    ToolBar toolBar = new ToolBar(composite, SWT.NONE);
    ToolItem item = new ToolItem(toolBar, SWT.PUSH);
    item.setImage(circle);
    item = new ToolItem(toolBar, SWT.PUSH);
    item.setImage(square);
    item = new ToolItem(toolBar, SWT.PUSH);
    item.setImage(star);
    item = new ToolItem(toolBar, SWT.PUSH);
    item.setImage(triangle);
    return toolBar;
  }
  /**
   * Creates the Combo
   * 
   * @param composite the parent composite
   * @return Control
   */
  private Control createCombo(Composite composite) {
    // A bug with Windows causes the Combo not to drop
    // down if you add it directly to the CoolBar.
    // To work around this, create a Composite, add the
    // Combo to it, and add the Composite to the CoolBar.
    // This should work both on Windows and on all other
    // platforms.
    Composite c = new Composite(composite, SWT.NONE);
    c.setLayout(new FillLayout());
    Combo combo = new Combo(c, SWT.DROP_DOWN);
    combo.add("Option One");
    combo.add("Option Two");
    combo.add("Option Three");
    return c;
  }
  /**
   * Creates two stacked buttons
   * 
   * @param composite the parent composite
   * @return Control
   */
  private Control createStackedButtons(Composite composite) {
    Composite c = new Composite(composite, SWT.NONE);
    c.setLayout(new GridLayout(1, false));
    new Button(c, SWT.PUSH).setText("Button One");
    new Button(c, SWT.PUSH).setText("Button Two");
    return c;
  }
  /**
   * Helper method to calculate the size of the cool item
   * 
   * @param item the cool item
   */
  private void calcSize(CoolItem item) {
    Control control = item.getControl();
    Point pt = control.ruputeSize(SWT.DEFAULT, SWT.DEFAULT);
    pt = item.ruputeSize(pt.x, pt.y);
    item.setSize(pt);
  }
  /**
   * 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"));
      square = new Image(shell.getDisplay(), new FileInputStream(IMAGE_PATH
          + "square.gif"));
      star = new Image(shell.getDisplay(), new FileInputStream(IMAGE_PATH
          + "star.gif"));
      triangle = new Image(shell.getDisplay(), new FileInputStream(IMAGE_PATH
          + "triangle.gif"));
    } catch (IOException e) {
      // Images not found; handle gracefully
    }
  }
  
  /**
   * Disposes the images
   */
  private void disposeImages() {
    if (circle != null)
      circle.dispose();
    if (square != null)
      square.dispose();
    if (star != null)
      star.dispose();
    if (triangle != null)
      triangle.dispose();
  }
  /**
   * The entry point for the application
   * 
   * @param args the command line arguments
   */
  public static void main(String[] args) {
    new CoolBarTest().run();
  }
}





Create a coolbar (relayout when resized)

/*
 * CoolBar example snippet: create a coolbar (relayout when resized)
 *
 * 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.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.CoolBar;
import org.eclipse.swt.widgets.CoolItem;
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 Snippet150 {
  static int itemCount;
  static CoolItem createItem(CoolBar coolBar, int count) {
    ToolBar toolBar = new ToolBar(coolBar, SWT.FLAT);
    for (int i = 0; i < count; i++) {
      ToolItem item = new ToolItem(toolBar, SWT.PUSH);
      item.setText(itemCount++ + "");
    }
    toolBar.pack();
    Point size = toolBar.getSize();
    CoolItem item = new CoolItem(coolBar, SWT.NONE);
    item.setControl(toolBar);
    Point preferred = item.ruputeSize(size.x, size.y);
    item.setPreferredSize(preferred);
    return item;
  }
  public static void main(String[] args) {
    Display display = new Display();
    final Shell shell = new Shell(display);
    CoolBar coolBar = new CoolBar(shell, SWT.NONE);
    createItem(coolBar, 3);
    createItem(coolBar, 2);
    createItem(coolBar, 3);
    createItem(coolBar, 4);
    int style = SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL;
    Text text = new Text(shell, style);
    FormLayout layout = new FormLayout();
    shell.setLayout(layout);
    FormData coolData = new FormData();
    coolData.left = new FormAttachment(0);
    coolData.right = new FormAttachment(100);
    coolData.top = new FormAttachment(0);
    coolBar.setLayoutData(coolData);
    coolBar.addListener(SWT.Resize, new Listener() {
      public void handleEvent(Event event) {
        shell.layout();
      }
    });
    FormData textData = new FormData();
    textData.left = new FormAttachment(0);
    textData.right = new FormAttachment(100);
    textData.top = new FormAttachment(coolBar);
    textData.bottom = new FormAttachment(100);
    text.setLayoutData(textData);
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }
    display.dispose();
  }
}





Drop-down a chevron menu containing hidden tool items

/*
 * CoolBar example snippet: drop-down a chevron menu containing hidden tool items
 *
 * 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.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.CoolBar;
import org.eclipse.swt.widgets.CoolItem;
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 Snippet140 {
  static Display display;
  static Shell shell;
  static CoolBar coolBar;
  static Menu chevronMenu = null;
  public static void main(String[] args) {
    display = new Display();
    shell = new Shell(display);
    shell.setLayout(new GridLayout());
    coolBar = new CoolBar(shell, SWT.FLAT | SWT.BORDER);
    coolBar.setLayoutData(new GridData(GridData.FILL_BOTH));
    ToolBar toolBar = new ToolBar(coolBar, SWT.FLAT | SWT.WRAP);
    int minWidth = 0;
    for (int j = 0; j < 5; j++) {
      int width = 0;
      ToolItem item = new ToolItem(toolBar, SWT.PUSH);
      item.setText("B" + j);
      width = item.getWidth();
      /* find the width of the widest tool */
      if (width > minWidth)
        minWidth = width;
    }
    CoolItem coolItem = new CoolItem(coolBar, SWT.DROP_DOWN);
    coolItem.setControl(toolBar);
    Point size = toolBar.ruputeSize(SWT.DEFAULT, SWT.DEFAULT);
    Point coolSize = coolItem.ruputeSize(size.x, size.y);
    coolItem.setMinimumSize(minWidth, coolSize.y);
    coolItem.setPreferredSize(coolSize);
    coolItem.setSize(coolSize);
    coolItem.addSelectionListener(new SelectionAdapter() {
      public void widgetSelected(SelectionEvent event) {
        if (event.detail == SWT.ARROW) {
          CoolItem item = (CoolItem) event.widget;
          Rectangle itemBounds = item.getBounds();
          Point pt = coolBar.toDisplay(new Point(itemBounds.x,
              itemBounds.y));
          itemBounds.x = pt.x;
          itemBounds.y = pt.y;
          ToolBar bar = (ToolBar) item.getControl();
          ToolItem[] tools = bar.getItems();
          int i = 0;
          while (i < tools.length) {
            Rectangle toolBounds = tools[i].getBounds();
            pt = bar
                .toDisplay(new Point(toolBounds.x, toolBounds.y));
            toolBounds.x = pt.x;
            toolBounds.y = pt.y;
            /*
             * Figure out the visible portion of the tool by looking
             * at the intersection of the tool bounds with the cool
             * item bounds.
             */
            Rectangle intersection = itemBounds
                .intersection(toolBounds);
            /*
             * If the tool is not completely within the cool item
             * bounds, then it is partially hidden, and all
             * remaining tools are completely hidden.
             */
            if (!intersection.equals(toolBounds))
              break;
            i++;
          }
          /*
           * Create a menu with items for each of the completely
           * hidden buttons.
           */
          if (chevronMenu != null)
            chevronMenu.dispose();
          chevronMenu = new Menu(coolBar);
          for (int j = i; j < tools.length; j++) {
            MenuItem menuItem = new MenuItem(chevronMenu, SWT.PUSH);
            menuItem.setText(tools[j].getText());
          }
          /*
           * Drop down the menu below the chevron, with the left edges
           * aligned.
           */
          pt = coolBar.toDisplay(new Point(event.x, event.y));
          chevronMenu.setLocation(pt.x, pt.y);
          chevronMenu.setVisible(true);
        }
      }
    });
    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }
    display.dispose();
  }
}





SWT CoolBar Test Demo

import org.eclipse.jface.window.ApplicationWindow;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.ruposite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.CoolBar;
import org.eclipse.swt.widgets.CoolItem;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;
public class SWTCoolBarTestDemo extends ApplicationWindow {
  public SWTCoolBarTestDemo() {
    super(null);
  }
  protected Control createContents(Composite parent) {
    // --- Create the window title. ---
    getShell().setText("CoolBar Test");
    String asCoolItemSection[] = { "File", "Formatting", "Search" };
    CoolBar composite = new CoolBar(parent, SWT.NONE);
    for (int idxCoolItem = 0; idxCoolItem < 3; ++idxCoolItem) {
      CoolItem item = new CoolItem(composite, SWT.NONE);
      ToolBar tb = new ToolBar(composite, SWT.FLAT);
      for (int idxItem = 0; idxItem < 3; ++idxItem) {
        ToolItem ti = new ToolItem(tb, SWT.NONE);
        ti
            .setText(asCoolItemSection[idxCoolItem] + " Item #"
                + idxItem);
      }
      Point p = tb.ruputeSize(SWT.DEFAULT, SWT.DEFAULT);
      tb.setSize(p);
      Point p2 = item.ruputeSize(p.x, p.y);
      item.setControl(tb);
      item.setSize(p2);
    }
    return composite;
  }
  public static void main(String[] args) {
    // --- Display SWTCoolBarTestDemo until the window is closed. ---
    SWTCoolBarTestDemo app = new SWTCoolBarTestDemo();
    app.setBlockOnOpen(true);
    app.open();
    Display.getCurrent().dispose();
  }
}





SWY CoolBarClass

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.CoolBar;
import org.eclipse.swt.widgets.CoolItem;
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 CoolBarClass {
  static Display display;
  static Shell shell;
  static CoolBar coolBar;
  static Menu menu = null;
  public static void main(String[] args) {
    display = new Display();
    shell = new Shell(display);
    shell.setLayout(new GridLayout());
    shell.setText("CoolBar Example");
    shell.setSize(600, 200);
    coolBar = new CoolBar(shell, SWT.BORDER | SWT.FLAT);
    coolBar.setLayoutData(new GridData(GridData.FILL_BOTH));
    ToolBar toolBar1 = new ToolBar(coolBar, SWT.FLAT);
    for (int loopIndex = 0; loopIndex < 5; loopIndex++) {
      ToolItem toolItem = new ToolItem(toolBar1, SWT.PUSH);
      toolItem.setText("Button " + loopIndex);
    }
    ToolBar toolBar2 = new ToolBar(coolBar, SWT.FLAT | SWT.WRAP);
    for (int loopIndex = 5; loopIndex < 10; loopIndex++) {
      ToolItem toolItem = new ToolItem(toolBar2, SWT.PUSH);
      toolItem.setText("Button " + loopIndex);
    }
    CoolItem coolItem1 = new CoolItem(coolBar, SWT.DROP_DOWN);
    coolItem1.setControl(toolBar1);
    CoolItem coolItem2 = new CoolItem(coolBar, SWT.DROP_DOWN);
    coolItem2.setControl(toolBar2);
    Point toolBar1Size = toolBar1.ruputeSize(SWT.DEFAULT, SWT.DEFAULT);
    Point coolBar1Size = coolItem1.ruputeSize(toolBar1Size.x,
        toolBar1Size.y);
    coolItem1.setSize(coolBar1Size);
    Point toolBar2Size = toolBar2.ruputeSize(SWT.DEFAULT, SWT.DEFAULT);
    Point coolBar2Size = coolItem1.ruputeSize(toolBar2Size.x,
        toolBar2Size.y);
    coolItem2.setSize(coolBar2Size);
    class CoolBarListener extends SelectionAdapter {
      public void widgetSelected(SelectionEvent event) {
        if (event.detail == SWT.ARROW) {
          ToolBar toolBar = (ToolBar) ((CoolItem) event.widget)
              .getControl();
          ToolItem[] buttons = toolBar.getItems();
          if (menu != null) {
            menu.dispose();
          }
          menu = new Menu(coolBar);
          for (int loopIndex = 0; loopIndex < buttons.length; loopIndex++) {
            MenuItem menuItem = new MenuItem(menu, SWT.PUSH);
            menuItem.setText(buttons[loopIndex].getText());
          }
          Point menuPoint = coolBar.toDisplay(new Point(event.x,
              event.y));
          menu.setLocation(menuPoint.x, menuPoint.y);
          menu.setVisible(true);
        }
      }
    }
    coolItem1.addSelectionListener(new CoolBarListener());
    coolItem2.addSelectionListener(new CoolBarListener());
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }
    display.dispose();
  }
}