Using ToolBars with CoolBars

A close examination of CoolbarShellExample reveals several issues. First, Button objects associated with the CoolBar are unable to use both text and an image, as was the case with ToolBar. Attempting to use both text and image results in only text being visible. Second, since the user can move CoolItems around on the CoolBar, you lose the ability to maintain functional groups of buttons unless you lock the CoolBar (which negates many of the reasons for using it in the first place).

These issues are easily solved once you realize that it is possible to add a ToolBar to a CoolBar as a single unit.

How do I do that?

Any ToolBar can be added to a CoolBar. All you must do is create the ToolBar in the normal manner (see Chapter 4), then create a CoolItem to serve as the holder for the ToolBar. Consider the modification to CoolbarShellExample shown in Example 15-2.

Example 15-2. Using a ToolBar with a CoolBar

import org.eclipse.swt.SWT; import org.eclipse.swt.graphics.Image; import org.eclipse.swt.graphics.Point; import org.eclipse.swt.widgets.*; public class CoolbarShellExample { Display d; Shell s; CoolbarShellExample( ) { d = new Display( ); s = new Shell(d); s.setSize(400,300); s.setImage(new Image(d, "c:\\icons\\JavaCup.ico")); s.setText("A Shell Coolbar Example"); final CoolBar coolBar = new CoolBar(s,SWT.BORDER); coolBar.setSize(395,70); coolBar.setLocation(0,0); // create images for toolbar buttons final Image saveIcon = new Image(d, "c:\\icons\\save.jpg"); ...

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

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.