Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-02-2008, 09:08 PM
Java Tip's Avatar
Moderator
 
Join Date: Nov 2007
Posts: 1,691
Rep Power: 5
Java Tip will become famous soon enoughJava Tip will become famous soon enough
Default How to use ToolBar in SWT
Code:
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.computeSize(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();
  }
}
__________________
"The sole cause of man’s unhappiness is that he does not know how to stay quietly in his room." - Blaise Pascal
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
NetBeans-style Dockable-windows and toolbar? jimm1 Advanced Java 0 01-31-2008 09:41 PM
how to create and install toolbar in a browser barney New To Java 1 08-07-2007 08:19 AM
Flash toolbar lenny New To Java 1 08-07-2007 07:17 AM
Toolbar Settings And Shortcuts In NetBeans IDE JavaForums NetBeans 0 08-02-2007 01:11 PM


All times are GMT +2. The time now is 12:19 AM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org