DekGenius.com
Previous Section  < Day Day Up >  Next Section

Recipe 8.7 Handling SWT Widget Events

8.7.1 Problem

You need to catch widget events such as button clicks and respond to them in code.

8.7.2 Solution

Use an SWT listener. In SWT, listeners are much like listeners in AWT. Here are some of the most popular SWT listeners:


ControlListener

Handles moving and resizing events


FocusListener

Handles getting and losing focus events


KeyListener

Handles keystroke events


MouseListener, MouseMoveListener, MouseTrackListener

Handles mouse events


SelectionListener

Handles widget selection events (including button clicks)

8.7.3 Discussion

To see how this works, we'll continue the example begun in the previous recipe where we want text to appear in a text widget when you click a button. You can catch button clicks by adding a SelectionListener object to the button with the addSelectionListener method. To implement the SelectionListener interface, you have to implement two methods: widgetSelected, which occurs when a selection occurs in a widget, and widgetDefaultSelected, which occurs when a default selection is made in a widget.

In this case, we're going to display the text No problem in the text widget using a SelectionListener object in an anonymous inner class:

public class ButtonClass {

    public static void main(String [] args) {
       Display display = new Display( );
       Shell shell = new Shell(display);
       shell.setSize(200, 200);
       shell.setText("Button Example");

       final Button button = new Button(shell, SWT.PUSH);
       button.setBounds(40, 50, 50, 20);
       button.setText("Click Me");

       final Text text = new Text(shell, SWT.BORDER);
       text.setBounds(100, 50, 70, 20);
       
       button.addSelectionListener(new SelectionListener( )
       {
          public void widgetSelected(SelectionEvent event)
          {
            text.setText("No problem");
          }

          public void widgetDefaultSelected(SelectionEvent event)
          {
            text.setText("No worries!");
          }
       });
        .
        .
        .

Adapter classes are available for every listener class, and you can extend your listener class from an adapter class instead of implementing the listener's interface. Doing so saves you the trouble of implementing all the listener class's methods. For example, the adapter class for the SelectionListener interface is named SelectionAdapter. For an example showing how this works, see Recipe 9.8.


All you need to complete this example is the code to close the shell when needed (see Example 8-5).

Example 8-5. Using SWT buttons and text widgets
package org.cookbook.ch08;

import org.eclipse.swt.widgets.*;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.*;

public class ButtonClass {

    public static void main(String [] args) {
       Display display = new Display( );
       Shell shell = new Shell(display);
       shell.setSize(200, 200);
       shell.setText("Button Example");

       final Button button = new Button(shell, SWT.PUSH);
       button.setBounds(40, 50, 50, 20);
       button.setText("Click Me");

       final Text text = new Text(shell, SWT.BORDER);
       text.setBounds(100, 50, 70, 20);
       
       button.addSelectionListener(new SelectionListener( )
       {

          public void widgetSelected(SelectionEvent event)
          {
            text.setText("No problem");
          }

          public void widgetDefaultSelected(SelectionEvent event)
          {
            text.setText("No worries!");
          }
       });

       shell.open( );
       while(!shell.isDisposed( )) {
          if(!display.readAndDispatch( )) display.sleep( );
       }
       display.dispose( );
    }
}

The results appear in Figure 8-6; when you click the button, the text message No problem appears in the text widget.

Figure 8-6. Using buttons and text widgets
figs/ecb_0806.gif


8.7.4 See Also

Recipe 8.1 on using widgets in SWT; Recipe 8.6 on creating button and text widgets.

    Previous Section  < Day Day Up >  Next Section