Using SWT Toolbars


The Browser project uses an SWT ToolBar widget to display a set of buttons, as you see in Figure 8.1. To create a toolbar in the SWT, you just connect the toolbar with the shell you're working with and set its style.

For example, take a look at the SWT ToolbarProject application, which appears in Figure 8.4. There are four buttons in the toolbar, and when the user clicks one, that button is identified in a message that appears in the Text widget.

Figure 8.4. Using a toolbar in an SWT application.


The ToolbarProject.java application creates a new Toolbar object this way (the SWT.NONE style indicates that the toolbar won't have any special styling; the allowable styles for toolbars are SWT.FLAT, SWT.NONE, SWT.WRAP, SWT.RIGHT, SWT.HORIZONTAL, SWT.VERTICAL, SWT.SHADOW_IN, and SWT.SHADOW_OUT):

 import org.eclipse.swt.*;  import org.eclipse.swt.widgets.*; public class ToolbarProject {     Display display;     Shell shell;     ToolBar toolbar;         .         .         .     public static void main(String [] args)     {         new ToolbarProject();     }     ToolbarProject()     {         display = new Display();         shell = new Shell(display);         shell.setText("SWT Toolbars");         shell.setSize(240, 200);         toolbar = new ToolBar(shell, SWT.NONE);         .         .         . } 

You can find the significant methods of the SWT Toolbar class in Table 8.5. The Browser project will use one of these ToolBar widgets.

Table 8.5. Significant Methods of the org.eclipse.swt.widgets.Toolbar Class

Method

Does This

Point computeSize(int wHint, int hHint, boolean changed)

Returns the preferred size of the ToolBar widget

Rectangle computeTrim(int x, int y, int width, int height)

Passing a specific client area to this method returns the bounding rectangle for the ToolBar widget required to enclose that client area

ToolItem getItem(int index)

Returns the item at the given index in the toolbar

ToolItem getItem(Point point)

Returns the item at the given point in the toolbar, or null if there is no item at that point

int getItemCount()

Returns the number of items the toolbar contains

ToolItem[] getItems()

Returns an array of ToolItem objects that are contained in the toolbar

int getRowCount()

Returns the number of rows in the ToolBar widget

int indexOf(ToolItem item)

Returns the index of the specified item in the toolbar

void setFont(Font font)

Sets the font that the ToolBar widget will use to draw text


Each of the four toolbar buttons you see in Figure 8.4 is created using an SWT ToolItem object. They're created with the style SWT.PUSH, to make them appear as push buttons (the allowable styles are SWT.PUSH, SWT.CHECK, SWT.RADIO, SWT.SEPARATOR, and SWT.DROP_DOWN):

 import org.eclipse.swt.*;  import org.eclipse.swt.widgets.*; public class ToolbarProject {     Display display;     Shell shell;     ToolBar toolbar;     ToolItem item1, item2, item3, item4;     public static void main(String [] args)     {         new ToolbarProject();     }     ToolbarProject()     {         display = new Display();         shell = new Shell(display);         shell.setText("SWT Toolbars");         shell.setSize(240, 200);         toolbar = new ToolBar(shell, SWT.NONE);         item1 = new ToolItem(toolbar, SWT.PUSH);         item1.setText("Item 1");         item2 = new ToolItem(toolbar, SWT.PUSH);         item2.setText("Item 2");         item3 = new ToolItem(toolbar, SWT.PUSH);         item3.setText("Item 3");         item4 = new ToolItem(toolbar, SWT.PUSH);         item4.setText("Item 4");        .         .         . } 

You can find the significant methods of the SWT ToolItem class in Table 8.6.

Table 8.6. Significant Methods of the org.eclipse.swt.widgets.ToolItem Class

Method

Does This

void addSelectionListener (SelectionListener listener)

Adds the given selection listener to the listeners that will be notified when the item is selected

Rectangle getBounds()

Returns a rectangle containing the item's size and location

Image getDisabledImage()

Returns the image used to indicate the item is disabled

boolean getEnabled()

Returns true if the item is enabled. Returns false otherwise

Image getHotImage()

Returns the image the item will display as the mouse moves over it

ToolBar getParent()

Returns the item's parent toolbar

boolean getSelection()

Returns true if the item is selected. Returns false otherwise

String getToolTipText()

Returns the item's tool tip text. Returns null if it has no tool tip text

int getWidth()

Returns the width of the item, in pixels.

boolean isEnabled()

Returns true if the item is enabled. Returns false otherwise

void removeSelectionListener (SelectionListener listener)

Removes the given listener from the listeners that will be notified when the item is selected

void setDisabledImage(Image image)

Sets the image that will appear in the item when it is disabled

void setEnabled(boolean enabled)

Enables the item if you pass a value of true. Disables it otherwise

void setHotImage(Image image)

Sets the image that will appear when the mouse passes over the item

void setImage(Image image)

Sets the image that will appear in the item

void setSelection(boolean selected)

Sets the selection state of the item

void setText(String string)

Sets the text that appears in the item

void setToolTipText(String string)

Sets the item's tool tip text

void setWidth(int width)

Sets the width of the item, in pixels


There are four buttons herehow can you tell which button was clicked? You use a Listener object and override the handleEvent method. This method is passed an Event object:

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

You can determine which button was selected by recovering the ToolItem widget that was clickedwhich you can do with the Event object's widget fieldand by getting that item's caption text:

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

Now you can display the appropriate message in a Text widget, based on which item was clicked, which you determine by checking the caption of the item:

 import org.eclipse.swt.*; import org.eclipse.swt.widgets.*; public class ToolbarProject {     Display display;     Shell shell;     ToolBar toolbar;     ToolItem item1, item2, item3, item4;     Text text;     public static void main(String [] args)     {         new ToolbarProject();     }     ToolbarProject()     {         display = new Display();         shell = new Shell(display);         shell.setText("SWT Toolbars");         shell.setSize(240, 200);         toolbar = new ToolBar(shell, SWT.NONE);         item1 = new ToolItem(toolbar, SWT.PUSH);         item1.setText("Item 1");         item2 = new ToolItem(toolbar, SWT.PUSH);         item2.setText("Item 2");         item3 = new ToolItem(toolbar, SWT.PUSH);         item3.setText("Item 3");         item4 = new ToolItem(toolbar, SWT.PUSH);         item4.setText("Item 4");         toolbar.setBounds(0, 0, 200, 40);         text = new Text(shell, SWT.BORDER);         text.setBounds(0, 60, 200, 25);         Listener listener = new Listener() {             public void handleEvent(Event event) {                 ToolItem item =(ToolItem)event.widget;                 String string = item.getText();{                 if(string.equals("Item 1"))                     text.setText("You clicked Item 1");                 else if(string.equals("Item 2"))                     text.setText("You clicked Item 2");                 else if(string.equals("Item 3"))                     text.setText("You clicked Item 3");                 else if(string.equals("Item 4"))                     text.setText("You clicked Item 4");             }         };         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();     } } 

That finishes ToolbarProject.java; you can see it at work in Figure 8.4. When the user clicks an item in the toolbar, the application reports which one was clicked. Not bad.

That gives you all the technology you need to create the Browser project, except for one itemthe Browser object itself. That's coming up next.



    Java After Hours(c) 10 Projects You'll Never Do at Work
    Java After Hours: 10 Projects Youll Never Do at Work
    ISBN: 0672327473
    EAN: 2147483647
    Year: 2006
    Pages: 128

    Similar book on Amazon

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