8.2 Working with Toolbars

     

8.2 Working with Toolbars

SWT also supports toolbars, using the Toolbar class. To create a toolbar, you just associate the toolbar with the shell you're working with and set its style:


SWT.BORDER

Creates a toolbar with a border


SWT.FLAT

Creates a flat toolbar


SWT.WRAP

Creates a wrappable toolbar


SWT.RIGHT

Aligns the toolbar on the right


SWT.SHADOW_OUT

Adds a shadow to the toolbar


SWT.HORIZONTAL

Creates a horizontal toolbar


SWT.VERTICAL

Creates a vertical toolbar

Here's how we'll create a new toolbar (the constant SWT.NONE indicates we're not setting a custom style):

 ToolBar toolbar = new ToolBar(shell, SWT.NONE); 

Each item in the toolbar is a ToolItem object, and we'll create four push buttons in an example next , using the SWT.PUSH style and the ToolItem class. Here are the possible styles for tool items:


SWT.PUSH

A push button


SWT.CHECK

A checkbox


SWT.RADIO

A radio button


SWT.SEPARATOR

A toolbar separator


SWT.DROP_DOWN

A drop-down item

You can set the text that will appear in the four buttonsin this case, the text will be Now, is, the, timethis way:

 ToolBar toolbar = new ToolBar(shell, SWT.NONE);  ToolItem item1 = new ToolItem(toolbar, SWT.PUSH);   item1.setText("Now");   ToolItem item2 = new ToolItem(toolbar, SWT.PUSH);   item2.setText("is");   ToolItem item3 = new ToolItem(toolbar, SWT.PUSH);   item3.setText("the");   ToolItem item4 = new ToolItem(toolbar, SWT.PUSH);   item4.setText("time");  

If you want to, you can also display images, disabled images, and hot images (displayed when the mouse moves over a toolbar item) in a toolbar item using an SWT Image object:

 ToolItem item1 = new ToolItem(toolbar, SWT.PUSH); item1.setImage(image); item1.setDisabledImage(disabledImage); item1.setHotImage(hotImage); 

We're also going to set the bounds of the toolbar in the shell and create a text control to display text matching the button the user has clicked:

 toolbar.setBounds(0, 0, 200, 70);              final Text text = new Text(shell, SWT.BORDER); text.setBounds(0, 100, 200, 25); 

You need to connect the various toolbar items to a listener to handle their events, and, in this case, we're going to use a generic Listener object. You can handle events from toolbar items with the Listener class's handleEvent method:

 Listener listener = new Listener( ) {     public void handleEvent(Event event) {        .        .        .    } }; 

This method is passed an event object, and you can extract the widget that actually caused the event with this object's widget member. To determine which toolbar item you've retrieved, you can recover the text in the toolbar item with the getText method, which retrieves the text from any widget, from text controls to toolbar buttons:

 Listener listener = new Listener( ) {     public void handleEvent(Event event) {  ToolItem item = (ToolItem)event.widget;   String string = item.getText( );  .             .             .    } }; 

To complete the listener, check what toolbar item was selected and display an appropriate message in text controlnote that when you have recovered the text from the button's caption, you know which button was clicked:

 Listener listener = new Listener( ) {     public void handleEvent(Event event) {         ToolItem item =(ToolItem)event.widget;         String string = item.getText( );  text.setText("You selected:" + string);  } }; 

That completes the listener, which you can add to each toolbar item with the addListener method. In this case, we're going to indicate that we want to handle the SWT.Selection event, as you see in the listing for Example 8-2.

Example 8-2. Using SWT toolbars
 package org.eclipse.ch08; import org.eclipse.swt.*; import org.eclipse.swt.widgets.*; public class Ch08_02 {         public static void main(String [] args) {             Display display = new Display( );             final Shell shell = new Shell(display);             shell.setSize(300, 200);                          ToolBar toolbar = new ToolBar(shell, SWT.NONE);             ToolItem item1 = new ToolItem(toolbar, SWT.PUSH);             item1.setText("Now");             ToolItem item2 = new ToolItem(toolbar, SWT.PUSH);             item2.setText("is");             ToolItem item3 = new ToolItem(toolbar, SWT.PUSH);             item3.setText("the");             ToolItem item4 = new ToolItem(toolbar, SWT.PUSH);             item4.setText("time");                           toolbar.setBounds(0, 0, 200, 70);                          final Text text = new Text(shell, SWT.BORDER);             text.setBounds(0, 100, 200, 25);                          Listener listener = new Listener( ) {                 public void handleEvent(Event event) {                     ToolItem item =(ToolItem)event.widget;                     String string = item.getText( );                     text.setText("You selected:" + string);               }             };  item1.addListener(SWT.Selection, listener);             item2.addListener(SWT.Selection, listener);             item3.addListener(SWT.Selection, listener);             item4.addListener(SWT.Selection, listener);             shell.open( );                      while (!shell.isDisposed( )) {                 if (!display.readAndDispatch( ))                     display.sleep( );             }             display.dispose( );  } } 

The results appear in Figure 8-3. Although the toolbar appears at the upper left, it's still a widget like any other. You can position the toolbar where you want it by using the setBounds method or by using one of the SWT layouts.

Figure 8-3. A new toolbar
figs/ecps_0803.gif

That's all you needwhen the user clicks an item in the toolbar, the application reports which items were clicked correctly, as you see in Figure 8-4.

Figure 8-4. Recovering a toolbar event
figs/ecps_0804.gif

It's worth noting that you can elaborate toolbars to support other controls as wellfor example, to add a combo box to a toolbar, you can use this code:

 Combo combo = new Combo(toolbar, SWT.READ_ONLY); 

As with any serious GUI package, toolbars can also display drop-down menus. In SWT, however, you support toolbar menus with context menus , setting their location and making them visible as needed using code like this:

 menu.setLocation(point.x, point.y); menu.setVisible(true); 


Eclipse
Eclipse
ISBN: 0596006411
EAN: 2147483647
Year: 2006
Pages: 114
Authors: Steve Holzner

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net