SWTToolBar.java Source code

Java tutorial

Introduction

Here is the source code for SWTToolBar.java

Source

/*
 * Created on Nov 20, 2003
 *
 * To change the template for this generated file go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */

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;

/**
 * @author Steven Holzner
 * 
 */
public class SWTToolBar {

    public static void main(String[] args) {
        Display display = new Display();
        final Shell shell = new Shell(display);
        shell.setSize(300, 200);

        ToolBar toolbar = new ToolBar(shell, SWT.NONE);
        ToolItem item1 = new ToolItem(toolbar, SWT.PUSH);
        item1.setText("Now");
        ToolItem item2 = new ToolItem(toolbar, SWT.PUSH);
        item2.setText("is");
        ToolItem item3 = new ToolItem(toolbar, SWT.PUSH);
        item3.setText("the");
        ToolItem item4 = new ToolItem(toolbar, SWT.PUSH);
        item4.setText("time");

        toolbar.setBounds(0, 0, 200, 70);

        final Text text = new Text(shell, SWT.BORDER);
        text.setBounds(0, 100, 200, 25);

        Listener listener = new Listener() {
            public void handleEvent(Event event) {
                ToolItem item = (ToolItem) event.widget;
                String string = item.getText();
                if (string.equals("Now"))
                    text.setText("You selected: Now");
                else if (string.equals("is"))
                    text.setText("You selected: is");
                else if (string.equals("the"))
                    text.setText("You selected: the");
                else if (string.equals("time"))
                    text.setText("You selected: time");
            }
        };

        item1.addListener(SWT.Selection, listener);
        item2.addListener(SWT.Selection, listener);
        item3.addListener(SWT.Selection, listener);
        item4.addListener(SWT.Selection, listener);

        shell.open();

        while (!shell.isDisposed()) {
            if (!display.readAndDispatch())
                display.sleep();
        }
        display.dispose();
    }
}