9.3. Creating Toolbars
Problem
You want to create a toolbar and handle toolbar button clicks.
Solution
Use the SWT Toolbar and
ToolItem classes.
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.
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).
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access