SWT Events

31 December 2007

In order to work with events in SWT, you create a listener specific to the event that you wish to capture. There is an interface for each listener that you can use (for example SelectionListener). There is also a class that will provide you with the event information (for example SelectionEvent). Within the listener that you create, you must implement the methods defined in the interface.

SelectionListener listener = new SelectionListener() 
{
	public void widgetSelected(SelectionEvent arg) 
	{
		System.out.println("Button Selected");
	}
 
	public void widgetDefaultSelected(SelectionEvent arg) 
	{
	}
};

If there is more than one method, you can create an adapter instead (ie. SelectionAdapter). An adapter implements the interface with empty methods so that you do not have to define code for methods that you do not need in your listener. Note that in example above for SelectionListener, there is not any code for the widgetDefaultSelected() method. In that case, you can create an adapter instead, and only implement the widgetSelected() method.

SelectionAdapter adapter = new SelectionAdapter() 
{
	public void widgetSelected(SelectionEvent arg) 
	{
		System.out.println("Button Selected");
	}
};

The second thing you need to do is add the listener to your widget. Each control has methods to add listeners (ie. addSelectionListener()). You pass your listener to this method, and you are all set to capture events.

Button button = new Button(shell, SWT.PUSH);
button.addSelectionListener(listener);
 
Or, for example above, you could have also used: 
button.addSelectionListener(adapter);

Below is the list SWT event Listeners

ArmListener
ControlListener
DisposeListener
FocusListener
HelpListener
KeyListener
MenuListener
ModifyListener
MouseListener
MouseMoveListener
MouseTrackListener
PaintListener
SelectionListener
ShellListener
TraverseListener
TreeListener
VerifyListener

Example

The example below creates a text field and two buttons OK and Cancel. If you click on OK it will dispaly OK on console and if you click on Cancel it will display Cancel on console. It uses SelectionAdapter class’s widgetSelected() method to find out which button is clicked.

import org.eclipse.swt.*;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.events.*;
import org.eclipse.swt.layout.*;
 
public class SWTButton {
 
	public static void main(String[] args) {
		Display display = new Display();
		Shell shell = new Shell(display);
		Label label = new Label(shell, SWT.NONE);
		label.setText("Enter your name:");
		Text text = new Text(shell, SWT.BORDER);
		text.setLayoutData(new RowData(100, SWT.DEFAULT));
		Button ok = new Button(shell, SWT.PUSH);
		ok.setText("OK");
		ok.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				System.out.println("OK");
			}
		});
		Button cancel = new Button(shell, SWT.PUSH);
		cancel.setText("Cancel");
		cancel.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				System.out.println("Cancel");
			}
		});
		shell.setDefaultButton(cancel);
		shell.setLayout(new RowLayout());
		shell.pack();
		shell.open();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
		display.dispose();
	}
}

Output:
SWTEvents

del.icio.us:SWT Events  digg:SWT Events  spurl:SWT Events  wists:SWT Events  simpy:SWT Events  newsvine:SWT Events  blinklist:SWT Events  furl:SWT Events  reddit:SWT Events  fark:SWT Events  blogmarks:SWT Events  Y!:SWT Events  smarking:SWT Events  magnolia:SWT Events  segnalo:SWT Events  gifttagging:SWT Events

Top Of Page | Trackback

If you found this page useful, consider linking to it. Simply copy and paste the code below into your web site.

It will look like this: SWT Events

Leave a Reply