The Browser project uses an SWT ToolBar widget to display a set of buttons, as you see in Figure 8.1. To create a toolbar in the SWT, you just connect the toolbar with the shell you're working with and set its style. For example, take a look at the SWT ToolbarProject application, which appears in Figure 8.4. There are four buttons in the toolbar, and when the user clicks one, that button is identified in a message that appears in the Text widget. Figure 8.4. Using a toolbar in an SWT application.The ToolbarProject.java application creates a new Toolbar object this way (the SWT.NONE style indicates that the toolbar won't have any special styling; the allowable styles for toolbars are SWT.FLAT, SWT.NONE, SWT.WRAP, SWT.RIGHT, SWT.HORIZONTAL, SWT.VERTICAL, SWT.SHADOW_IN, and SWT.SHADOW_OUT): import org.eclipse.swt.*; import org.eclipse.swt.widgets.*; public class ToolbarProject { Display display; Shell shell; ToolBar toolbar; . . . public static void main(String [] args) { new ToolbarProject(); } ToolbarProject() { display = new Display(); shell = new Shell(display); shell.setText("SWT Toolbars"); shell.setSize(240, 200); toolbar = new ToolBar(shell, SWT.NONE); . . . } You can find the significant methods of the SWT Toolbar class in Table 8.5. The Browser project will use one of these ToolBar widgets.
Each of the four toolbar buttons you see in Figure 8.4 is created using an SWT ToolItem object. They're created with the style SWT.PUSH, to make them appear as push buttons (the allowable styles are SWT.PUSH, SWT.CHECK, SWT.RADIO, SWT.SEPARATOR, and SWT.DROP_DOWN): import org.eclipse.swt.*; import org.eclipse.swt.widgets.*; public class ToolbarProject { Display display; Shell shell; ToolBar toolbar; ToolItem item1, item2, item3, item4; public static void main(String [] args) { new ToolbarProject(); } ToolbarProject() { display = new Display(); shell = new Shell(display); shell.setText("SWT Toolbars"); shell.setSize(240, 200); toolbar = new ToolBar(shell, SWT.NONE); item1 = new ToolItem(toolbar, SWT.PUSH); item1.setText("Item 1"); item2 = new ToolItem(toolbar, SWT.PUSH); item2.setText("Item 2"); item3 = new ToolItem(toolbar, SWT.PUSH); item3.setText("Item 3"); item4 = new ToolItem(toolbar, SWT.PUSH); item4.setText("Item 4"); . . . } You can find the significant methods of the SWT ToolItem class in Table 8.6.
There are four buttons herehow can you tell which button was clicked? You use a Listener object and override the handleEvent method. This method is passed an Event object: Listener listener = new Listener() { public void handleEvent(Event event) { . . . } }; You can determine which button was selected by recovering the ToolItem widget that was clickedwhich you can do with the Event object's widget fieldand by getting that item's caption text: Listener listener = new Listener() { public void handleEvent(Event event) { ToolItem item =(ToolItem)event.widget; String string = item.getText(); . . . { } }; Now you can display the appropriate message in a Text widget, based on which item was clicked, which you determine by checking the caption of the item: import org.eclipse.swt.*; import org.eclipse.swt.widgets.*; public class ToolbarProject { Display display; Shell shell; ToolBar toolbar; ToolItem item1, item2, item3, item4; Text text; public static void main(String [] args) { new ToolbarProject(); } ToolbarProject() { display = new Display(); shell = new Shell(display); shell.setText("SWT Toolbars"); shell.setSize(240, 200); toolbar = new ToolBar(shell, SWT.NONE); item1 = new ToolItem(toolbar, SWT.PUSH); item1.setText("Item 1"); item2 = new ToolItem(toolbar, SWT.PUSH); item2.setText("Item 2"); item3 = new ToolItem(toolbar, SWT.PUSH); item3.setText("Item 3"); item4 = new ToolItem(toolbar, SWT.PUSH); item4.setText("Item 4"); toolbar.setBounds(0, 0, 200, 40); text = new Text(shell, SWT.BORDER); text.setBounds(0, 60, 200, 25); Listener listener = new Listener() { public void handleEvent(Event event) { ToolItem item =(ToolItem)event.widget; String string = item.getText();{ if(string.equals("Item 1")) text.setText("You clicked Item 1"); else if(string.equals("Item 2")) text.setText("You clicked Item 2"); else if(string.equals("Item 3")) text.setText("You clicked Item 3"); else if(string.equals("Item 4")) text.setText("You clicked Item 4"); } }; 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(); } } That finishes ToolbarProject.java; you can see it at work in Figure 8.4. When the user clicks an item in the toolbar, the application reports which one was clicked. Not bad. That gives you all the technology you need to create the Browser project, except for one itemthe Browser object itself. That's coming up next. |