• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

Chapter 4. SWT Toolbars > Creating the Toolbar

4.1. 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.


4.1.1. 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);

4.1.2. 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


4.1.3. 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);

4.1.4. 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


4.1.5. 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.


4.1.6. 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);

4.1.7. 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


4.1.8. 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.

  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint