Professional Documents
Culture Documents
SWTCH 04
SWTCH 04
Chapter 4
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 welldesigned windowthe 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 menuand almost every application willit 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 creationthe 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 buttons image, just as it specified a window icon for your shell back in Chapter 2.
In this chapter: Creating the Toolbar Creating Toolbar Buttons Creating a Complete Toolbar Example Making the Buttons Work Adding Listeners Adding Separators to Your Toolbar Adding Check and Radio ToolItems Determining Whether a Check or Radio Button Is Selected Using Tool Tips
55
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.
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.
How do I do that?
First, create the toolbar itselfan 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 about
using other styles for your toolbar? You can use five styles with the ToolBar class.
56
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 stylesfor example, the RIGHT and WRAP stylescan be combined to achive a desire effect.
Most toolbar buttons make use of assigned images rather than text labels.
How do I do that?
The ToolItem class constructor accepts as arguments a reference to the containing ToolBar, and the style of the button you wish to create. There are five styles you can use when creating ToolItem buttons: SWT.PUSH Creates a clickable toolbar button. SWT.CHECK Creates a toolbar button that toggles between selected and deselected.
57
SWT.RADIO Creates a toolbar button that is part of a group of buttons in which only one button can be selected. SWT.SEPARATOR Adds spacing between toolbar buttons. SWT.DROP_DOWN Creates a toolbar button to which a drop-down menu can be attached. The code needed to create a text-only button is simple:
final ToolItem textItem = new ToolItem(bar, SWT.PUSH); textItem.setText("Open Child");
Adding an image to a ToolItem is very similar to adding text, except that you use the setImage( ) method rather than the setText( ) method. First, you must create an instance of the Image class to load up the picture; then you associate that image with the ToolItem:
Image icon = new Image(d, "c:\\icons\\JavaCup.ico"); final ToolItem imageItem = new ToolItem(bar, SWT.PUSH); imageItem.setImage(icon);
Creating ToolItem buttons that use both images and text is simply a matter of calling both the setImage( ) and setText( ) methods:
Image icon = new Image(d, "c:\\icons\\JavaCup.ico"); final ToolItem textImageItem = new ToolItem(bar, SWT.PUSH); textImageItem.setImage(icon); textImageItem.setText("Java Icon");
58
How do I do that?
Example 4-1 demonstrates how to create a window, complete with both toolbar and menu.
Example 4-1. Creating a Toolbar example
import import import import org.eclipse.swt.SWT; org.eclipse.swt.events.*; org.eclipse.swt.graphics.Image; org.eclipse.swt.widgets.*;
public class ToolbarShellExample { Display d; Shell s; ToolbarShellExample( ) { d = new Display( ); s = new Shell(d); s.setSize(300,300); s.setImage(new Image(d, "c:\\icons\\JavaCup.ico")); s.setText("A Shell Toolbar Example"); final ToolBar bar = new ToolBar(s,SWT.HORIZONTAL); bar.setSize(300,70); bar.setLocation(0,0); // create images for toolbar buttons final Image saveIcon = new Image(d, "c:\\icons\\save.jpg"); final Image openIcon = new Image(d, "c:\\icons\\open.jpg"); final Image cutIcon = new Image(d, "c:\\icons\\cut.jpg"); final Image copyIcon = new Image(d, "c:\\icons\\copy.jpg"); final Image pasteIcon = new Image(d, "c:\\icons\\paste.jpg"); // create and add the button for performing an open operation final ToolItem openToolItem = new ToolItem(bar, SWT.PUSH); openToolItem.setImage(openIcon); openToolItem.setText("Open"); openToolItem.setToolTipText("Open File"); //create and add the button for performing a save operation final ToolItem saveToolItem = new ToolItem(bar, SWT.PUSH); saveToolItem.setImage(saveIcon); saveToolItem.setText("Save"); saveToolItem.setToolTipText("Save File"); //create and add the button for performing a cut operation final ToolItem cutToolItem = new ToolItem(bar, SWT.PUSH); cutToolItem.setImage(cutIcon); cutToolItem.setText("Cut"); cutToolItem.setToolTipText("Cut"); // create and add the button for performing a copy operation final ToolItem copyToolItem = new ToolItem(bar, SWT.PUSH); copyToolItem.setImage(copyIcon);
59
60
saveMenuItem.addSelectionListener(new SelectionListener( ) { public void widgetSelected(SelectionEvent e) { System.out.println("Save"); } public void widgetDefaultSelected(SelectionEvent e) { } }); exitMenuItem.addSelectionListener(new SelectionListener( ) { public void widgetSelected(SelectionEvent e) { System.exit(0); } public void widgetDefaultSelected(SelectionEvent e) { } }); cutMenuItem.addSelectionListener(new SelectionListener( ) { public void widgetSelected(SelectionEvent e) { System.out.println("Cut"); } public void widgetDefaultSelected(SelectionEvent e) { } }); copyMenuItem.addSelectionListener(new SelectionListener( ) { public void widgetSelected(SelectionEvent e) { System.out.println("Copy"); } public void widgetDefaultSelected(SelectionEvent e) { } }); pasteMenuItem.addSelectionListener(new SelectionListener( ) { public void widgetSelected(SelectionEvent e) { System.out.println("Paste"); } public void widgetDefaultSelected(SelectionEvent e) {
61
As with menus, creating toolbars takes a lot of code, though fewer objects. Toolbars are expensive, but much less so than menus.
62
How do I do that?
The Listener code for the four toolbar buttons created by Example 4-1 is shown here:
openToolItem.addSelectionListener(new SelectionListener( ) { public void widgetSelected(SelectionEvent event) { System.out.println("Open"); } public void widgetDefaultSelected(SelectionEvent event) { } }); saveToolItem.addSelectionListener(new SelectionListener( ) { public void widgetSelected(SelectionEvent event) { System.out.println("Save"); } public void widgetDefaultSelected(SelectionEvent event) { } }); cutToolItem.addSelectionListener(new SelectionListener( ) { public void widgetSelected(SelectionEvent event) { System.out.println("Cut"); } public void widgetDefaultSelected(SelectionEvent event) { } }); copyToolItem.addSelectionListener(new SelectionListener( ) { public void widgetSelected(SelectionEvent event) { System.out.println("Copy"); } public void widgetDefaultSelected(SelectionEvent event) {
63
} }); pasteToolItem.addSelectionListener(new SelectionListener( ) { public void widgetSelected(SelectionEvent event) { System.out.println("Paste"); } public void widgetDefaultSelected(SelectionEvent event) { } });
Adding the preceding code to the constructor for ToolbarShellExample results in the toolbar buttons becoming functionalat least to the point of printing a message to the Console when they are pressed, as shown in Figure 4-2.
What about
all that duplicate code in the SelectionListener classes? One of the goals of object-oriented programming is to reduce the amount of code that is duplicated in an application, yet as you see, the widgetSelected( ) code added to the ToolItem objects is identical to that added to the MenuItem objects created in Example 4-1.
64
This can be corrected by creating named inner classes that implement the SelectionListener interface, as shown in Example 4-2.
Example 4-2. Using named inner classes for SelectionListeners
import import import import import org.eclipse.swt.SWT; org.eclipse.swt.events.SelectionEvent; org.eclipse.swt.events.SelectionListener; org.eclipse.swt.graphics.Image; org.eclipse.swt.widgets.*;
public class ToolbarShellExample { Display d; Shell s; ToolbarShellExample( ) { d = new Display( ); s = new Shell(d); s.setSize(300,300); s.setImage(new Image(d, "c:\\icons\\JavaCup.ico")); s.setText("A Shell Toolbar Example"); final ToolBar bar = new ToolBar(s,SWT.HORIZONTAL); bar.setSize(500,70); bar.setLocation(0,0); // create images for toolbar buttons final Image saveIcon = new Image(d, "c:\\icons\\save.jpg"); final Image openIcon = new Image(d, "c:\\icons\\open.jpg"); final Image childIcon = new Image(d, "c:\\icons\\userH.ico"); final Image cutIcon = new Image(d, "c:\\icons\\cut.jpg"); final Image copyIcon = new Image(d, "c:\\icons\\copy.jpg"); final Image pasteIcon = new Image(d, "c:\\icons\\paste.jpg"); // create and add the button for performing an open operation final ToolItem openToolItem = new ToolItem(bar, SWT.PUSH); openToolItem.setImage(openIcon); openToolItem.setText("Open"); openToolItem.setToolTipText("Open File"); //create and add the button for performing a save operation final ToolItem saveToolItem = new ToolItem(bar, SWT.PUSH); saveToolItem.setImage(saveIcon); saveToolItem.setText("Save"); saveToolItem.setToolTipText("Save File"); final ToolItem sep1 = new ToolItem(bar, SWT.SEPARATOR); //create and add the button for performing a cut operation final ToolItem cutToolItem = new ToolItem(bar, SWT.PUSH); cutToolItem.setImage(cutIcon); cutToolItem.setText("Cut"); cutToolItem.setToolTipText("Cut"); // create and add the button for performing a copy operation final ToolItem copyToolItem = new ToolItem(bar, SWT.PUSH);
65
66
67
68
The inner class is then used for both the menu item and the toolbar button by calling the addSelectionListener( ) method for each:
openToolItem.addSelectionListener(new Open( )); openMenuItem.addSelectionListener(new Open( ));
This saves a little codehow much depends on the contents of the inner classes. Remember, he who writes the fewest lines wins.
Either approach is valid: choose whichever best matches your coding philosophy.
69
each other as the user gazes at the toolbar. To provide that visual distinction, you must add a separator.
How do I do that?
Separators are easy to create:
final ToolItem separator = new ToolItem(bar, SWT.SEPARATOR);
If you insert this line of code in the location where you wish the separator to appear (between the code that creates the Save button and the code that creates the Cut button), the toolbar appears as in Figure 4-3 a distinct improvement.
70
How do I do that?
As with all widgets, you can change the appearance and functionality of ToolItem objects simply by changing the style. Consider the following code:
final ToolItem sep2 = new ToolItem(bar, SWT.SEPARATOR); final ToolItem checkItem = new ToolItem(bar, SWT.CHECK); checkItem.setText("Check"); final ToolItem sep3 = new ToolItem(bar, SWT.SEPARATOR); final ToolItem radioItem1 = new ToolItem(bar, SWT.RADIO); radioItem1.setText("Radio 1"); final ToolItem radioItem2 = new ToolItem(bar, SWT.RADIO); radioItem2.setText("Radio 2"); final ToolItem sep4 = new ToolItem(bar, SWT.SEPARATOR); final ToolItem radioItem3 = new ToolItem(bar, SWT.RADIO); radioItem3.setText("Radio 3"); final ToolItem radioItem4 = new ToolItem(bar, SWT.RADIO); radioItem4.setText("Radio 4");
Adding this code to the version of ToolbarShellExample created by Example 4-1 results in the window shown in Figure 4-4.
This kind of toolbar button is extremely popular: try to find an application today that doesnt make some use of it.
Figure 4-4. Toolbar with check- and radio-style buttons
71
given time. Buttons that are immediately adjacent to each other are considered to be part of a group. If another ToolItem is created between two radio buttonseven a separatorthey will reside in two different groups. In Figure 4-4, there are two radio groups. The buttons labeled in the code Radio 1 and Radio 2 are in one group, while those labeled Radio 3 and Radio 4 are in a second group. You may select either Radio One or Radio Two, and either Radio Three or Radio Four, but you may not select Radio One and Radio Two, or Radio Three and Radio Four.
CHECK- and RADIO-style menu items work the same way, so the code required to determine status is similar.
How do I do that?
In a SelectionListeneror any other block of codeyou can determine whether a check or radio item is selected by calling the ToolItem getSelection( ) method.:
checkItem.addSelectionListener(new SelectionListener( ) { public void widgetSelected(SelectionEvent event) { if(checkItem.getSelection( )) { System.out.println("Check"); } else { System.out.println("Uncheck"); } } public void widgetDefaultSelected(SelectionEvent event)
72
{ } });
The code you develop for the widgetSelected( ) method depends upon the application. In place of the call to System.out.println( ), you could set a boolean instance variable that could be used to govern other program logical flow. Or you could define all tool items at the class level and call getSelection( ) whenever needed.
How do I do that?
SWT makes it very easy to create tool tips. All you must do is assign the desired text to a ToolItem by calling the setToolTipText( ) method, and pass the method the String value you wish to appear as the tool tip. Adding this line to your ToolbarShellExample results in Figure 4-6 when the mouse cursor is moved over the Open button:
openItem.setToolTipText("Open File");
The SWT provides you with everything you need to create sophisticated toolbars, something your users will expect in almost every application you deliver. As you have seen in the last three chapters, the SWT makes it easy to build the windows that serve as the foundation of your applications.
73