Working with Buttons

Our next step is going to be adding interactive widgets to SWT apps. In this case, we're going to add a button that, when clicked, will display text in a text control. We start off as before, except this time, we also import org.eclipse.swt.events.* to handle button clicks and org.eclipse.swt.layout.* to set the layout of our controls. And we can also embellish our example a little more by setting the text that should appear in the shell's titlebar, using the setText method:

package org.eclipsebook.ch07;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.*;
import org.eclipse.swt.layout.*;
 public static void main(String [] args) {
 Display display = new Display( );
 Shell shell = new Shell(display);
 shell.setSize(300, 200);
 shell.setLayout(new RowLayout( ));
 shell.setText("Button Example");
 .
 .
 .


Now we're dealing with multiple controls, and we're also going to set the SWT layout of our shell to the row layout, which displays controls in rows:

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


The row layout is only one of several layouts that lets you specify how controls are arranged; we'll see the others in the next section. Now we'll add the button using the Button class and the text control using the Text class. Here are the possible styles for buttons:


SWT.BORDER

Adds a border


SWT.CHECK

Creates a check button


SWT.PUSH

Creates a push button


SWT.RADIO

Creates a radio button


SWT.TOGGLE

Creates a toggle button


SWT.FLAT

Creates a flat button


SWT.LEFT

Sets left alignment


SWT.RIGHT

Sets right alignment


SWT.CENTER

Sets center alignment


SWT.ARROW

Creates an arrow button

And here are the possible styles for text controls:


SWT.BORDER

Adds a border


SWT.SINGLE

Allows single selections


SWT.READ_ONLY

Supports only read-only text


SWT.LEFT

Sets left-alignment


SWT.CENTER

Sets center alignment


SWT.RIGHT

Sets right alignment


SWT.WRAP

Allows text to wrap


SWT.MULTI

Allows multiple selections

Here's how we add the button and text control in code:

public class Ch07_02 {
 public static void main(String [] args) {
 Display display = new Display( );
 Shell shell = new Shell(display);
 shell.setSize(300, 200);
 shell.setText("Button Example");
 shell.setLayout(new RowLayout( ));
 final Button button = new Button(shell, SWT.PUSH);
 button.setText("Click Me");
 final Text text = new Text(shell, SWT.SHADOW_IN);
 .
 .
 .


To handle button clicks, you add a SelectionListener to the button, which you can do with the addSelectionListener method. Listeners in SWT are much like listeners in AWT; here are some of the most popular:


ControlListener

Handles moving and resizing


FocusListener

Handles the getting and losing of the focus


KeyListener

Handles key strokes


MouseListener , MouseMoveListener, MouseTrackListener

Handles the mouse


SelectionListener

Handles widget selections (including button clicks)

The SelectionListener interface has two methods that you have to implement: widgetSelected, when a selection occurs in a control, and widgetDefaultSelected, when a default selection is made in a control. Here, we're just going to display the text "No worries!" in a text control using an anonymous inner class:

button.addSelectionListener(new SelectionListener( )
{
 public void widgetSelected(SelectionEvent event)
 {
 text.setText("No worries!");
 }
 public void widgetDefaultSelected(SelectionEvent event)
 {
 text.setText("No worries!");
 }
});


All that's left is to display the shell and implement the event loop, as you see in Example 7-2.

Example 7-2. Using SWT buttons
package org.eclipsebook.ch07;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.*;
import org.eclipse.swt.layout.*;
public class Ch07_02 {
 public static void main(String [] args) {
 Display display = new Display( );
 Shell shell = new Shell(display);
 shell.setSize(300, 200);
 shell.setLayout(new RowLayout( ));
 shell.setText("Button Example");
 final Button button = new Button(shell, SWT.PUSH);
 button.setText("Click Me");
 final Text text = new Text(shell, SWT.SHADOW_IN);
 button.addSelectionListener(new SelectionListener( )
 {
 public void widgetSelected(SelectionEvent event)
 {
 text.setText("No worries!");
 }
 public void widgetDefaultSelected(SelectionEvent event)
 {
 text.setText("No worries!");
 }
 });
 shell.open( );
 while(!shell.isDisposed( )) {
 if(!display.readAndDispatch( )) display.sleep( );
 }
 display.dispose( );
 }
}


You can see the results in Screenshot-2; when the user clicks the button, the text message appears in the text control.

Screenshot-2. Using a button and text control
Java figs/ecps_0702.gif

Although we've used a row layout here, you can also directly set the bounds of controls with the setBounds(x, y, width, height) method. For example, if you want to explicitly set the location and sizes of the button and text control here, you might omit the call to shell.setLayout(new RowLayout( )) and use the setBounds method instead, like this:

public static void main (String [] args) {
 Display display = new Display ( );
 Shell shell = new Shell (display);
 shell.setSize(300, 200);
 shell.setText("Button Example");
 final Button button = new Button(shell, SWT.PUSH);
 button.setText("Click Me");
 button.setBounds(80, 80, 90, 20);
 final Text text = new Text (shell, SWT.SHADOW_IN);
 text.setBounds(180, 80, 90, 20);


Also, now that you're adding interactive widgets to your apps, you should know more about how widgets are handled with threads in SWT apps. In an SWT app, the main thread, the UI thread, is responsible for handling events and dispatching them to the correct widget. In AWT and Swing, you don't have to deal with the UI thread, but in SWT, the UI thread acts like a message pump that dispatches events to widgets as needed (setting things up this way makes it possible to use SWT plug-ins in Eclipse). The UI thread is the app's main thread, so if you want to perform a lot of heavy lifting, you should start other worker threads to do the work. The UI thread is also the only thread that can interact with widgets and not throw an SWTException exception. If you do start other threads, you can update the user interface when their tasks are done with the asyncExec and syncExec methods. You pass these methods a new Runnable object that does the updating you want. For example, in a worker thread, you could get the current Display object using the Display.getDefault method and set the text in a label, like this:

Display.getDefault( ).asyncExec(new Runnable( )
{
 public void run( )
 {
 label.setText("No worries!");
 }
});
      
Comments