June 2004
Beginner to intermediate
364 pages
7h 38m
English
You want to catch toolbar click events.
Add a listener to the toolbar buttons. Just create a new
Listener object, and use the toolbar
button’s addListener method to
add that listener.
To complete the example discussed in the two previous recipes, create
a new listener of the Listener class to handle the
buttons, handle click events by displaying the caption of the clicked
button (which you can get from the tool item’s
getText method), and add the new listener to the
toolbar buttons. You can see how that works in Example 9-2.
Example 9-2. Creating toolbars
package org.cookbook.ch09;
import org.eclipse.swt.*;
import org.eclipse.swt.widgets.*;
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 ...Read now
Unlock full access