Chapter 4. SWT Toolbars

To complete the examination of building windows using the SWT you must look at the final element that end users expect to see in a well-designed window—the toolbar. A toolbar is a row of buttons that appears below the menu and above the working area of the window to allow one-click access to the most commonly used functionality of your application. If your application supports the use of a menu—and almost every application will—it can benefit from the use of a toolbar.

Toolbar buttons can have a text label, an image, or both; the use of images that provide a hint about the functionality of the button is a nice touch. If you examine other applications running on your chosen platform, you will likely find that the use of images, rather than text, is the norm. Toolbar buttons also often make use of pop-up Help text that appears whenever the mouse hovers over the button.

SWT provides two classes that you use in toolbar creation—the ToolBar and ToolItem classes. These classes are part of the org.eclipse.swt.widgets package. The Image class, located in the org.eclipse.swt.graphics package, is also used to load and specify an icon or bitmap for use as a toolbar button’s image, just as it specified a window icon for your shell back in Chapter 2.

Creating the Toolbar

Creating toolbars is much like creating menus. Simply create an instance of the ToolBar class to serve as the container for the individual buttons, which are represented by instances of the ToolItem class.

Once you are armed with a list of buttons that you wish to appear on the toolbar, and you have acquired or developed images for those buttons, you are ready to begin development of your toolbar.

Note

Toolbars should be thoughtfully designed prior to development. There is only a small amount of space at the top of the window, so putting some thought into these decisions ahead of time (and/or talking it over with the intended user base) can save hours of programming time down the road.

How do I do that?

First, create the toolbar itself—an instance of the ToolBar class:

final ToolBar bar = new ToolBar(s, SWT.HORIZONTAL);

This single line of code creates an instance of ToolBar and attaches it to a shell, referenced by the variable s.

Next, set the size and location of the toolbar:

bar.setSize(300, 65);
bar.setLocation(0,0);

What just happened?

The first line sets the toolbar’s width and height. Generally, you should specify a width equal to the window’s width and a height that is of sufficient size to accommodate the buttons that will be placed on the toolbar. This, of course, will depend on the size of images you plan to use for those buttons, so once again planning before coding is required.

The second line sets the X and Y coordinates of the toolbar’s upper left-hand corner relative to the working area of the window. Generally, if you want the toolbar to be positioned directly below the menu, set the toolbar’s location to 0, 0. If you’re using a vertical toolbar, set the parameters to values that would make the toolbar position itself along either the left or right of the window. The right side position can be calculated by subtracting the width of the toolbar from the width of the window

Tip

Be careful when assigning a height (the second parameter). Although assigning more height than is necessary for the toolbar buttons won’t affect the toolbar’s appearance, it will decrease the amount of working area in the window for placing other components

How do I do that?

First, create the toolbar itself—an instance of the ToolBar class:

final ToolBar bar = new ToolBar(s, SWT.HORIZONTAL);

This single line of code creates an instance of ToolBar and attaches it to a shell, referenced by the variable s.

Next, set the size and location of the toolbar:

bar.setSize(300, 65);
bar.setLocation(0,0);

What just happened?

The first line sets the toolbar’s width and height. Generally, you should specify a width equal to the window’s width and a height that is of sufficient size to accommodate the buttons that will be placed on the toolbar. This, of course, will depend on the size of images you plan to use for those buttons, so once again planning before coding is required.

The second line sets the X and Y coordinates of the toolbar’s upper left-hand corner relative to the working area of the window. Generally, if you want the toolbar to be positioned directly below the menu, set the toolbar’s location to 0, 0. If you’re using a vertical toolbar, set the parameters to values that would make the toolbar position itself along either the left or right of the window. The right side position can be calculated by subtracting the width of the toolbar from the width of the window

Tip

Be careful when assigning a height (the second parameter). Although assigning more height than is necessary for the toolbar buttons won’t affect the toolbar’s appearance, it will decrease the amount of working area in the window for placing other components

What about...

using other styles for your toolbar? You can use five styles with the ToolBar class.

SWT.FLAT

Creates a non-3D toolbar.

SWT.HORIZONTAL

Note

Positions the toolbar at the top of the window, below the menu bar.

How do I do that?

First, create the toolbar itself—an instance of the ToolBar class:

final ToolBar bar = new ToolBar(s, SWT.HORIZONTAL);

This single line of code creates an instance of ToolBar and attaches it to a shell, referenced by the variable s.

Next, set the size and location of the toolbar:

bar.setSize(300, 65);
bar.setLocation(0,0);

What just happened?

The first line sets the toolbar’s width and height. Generally, you should specify a width equal to the window’s width and a height that is of sufficient size to accommodate the buttons that will be placed on the toolbar. This, of course, will depend on the size of images you plan to use for those buttons, so once again planning before coding is required.

The second line sets the X and Y coordinates of the toolbar’s upper left-hand corner relative to the working area of the window. Generally, if you want the toolbar to be positioned directly below the menu, set the toolbar’s location to 0, 0. If you’re using a vertical toolbar, set the parameters to values that would make the toolbar position itself along either the left or right of the window. The right side position can be calculated by subtracting the width of the toolbar from the width of the window

Tip

Be careful when assigning a height (the second parameter). Although assigning more height than is necessary for the toolbar buttons won’t affect the toolbar’s appearance, it will decrease the amount of working area in the window for placing other components

What about...

using other styles for your toolbar? You can use five styles with the ToolBar class.

SWT.FLAT

Creates a non-3D toolbar.

SWT.HORIZONTAL

Positions the toolbar at the top of the window, below the menu bar.

SWT.WRAP

Causes the toolbar item text to wrap across multiple lines.

SWT.RIGHT

Causes the text on toolbar items to be right-justified (appearing to the right of any image) .

SWT.VERTICAL

Positions the toolbar vertically along either side of the window, depending upon the parameters passed to the setLocation() method.

Some styles—for example, the RIGHT and WRAP styles—can be combined to achive a desire effect.

Get SWT: A Developer's Notebook now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.