Recipe 9.3 Creating Toolbars 9.3.1 Problem You want to create a toolbar and handle toolbar button clicks. 9.3.2 Solution Use the SWT Toolbar and ToolItem classes. 9.3.3 Discussion We'll create an example toolbar in a new project ( ToolbarApp at this book's site) and stock it with toolbar buttons , reporting which button the user clicked. To create a toolbar, you use the SWT Toolbar class; here's a selection of this class's most popular methods : -
- int getItemCount( )
-
Returns the number of items contained in the toolbar -
- ToolItem[] getItems( )
-
Returns an array of the items in the toolbar -
- int indexOf(ToolItem item)
-
Searches the toolbar until an item is found that is equal to the argument, and returns the index of that item Here's how you can create a new SWT toolbar: 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); . . . The next step is to add some buttons to the toolbar; see the following recipe for the details. 9.3.4 See Also Recipe 9.4 on embedding buttons in toolbars; Recipe 9.5 on handling toolbar events; Recipe 9.6 on embedding combo boxes, text widgets, and menus in toolbars; Chapter 8 in Eclipse (O'Reilly). |