View RSS Feed

My Java Tips

SWT Events

Rate this Entry
by , 11-10-2011 at 05:41 PM (1993 Views)
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.



Java Code:
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.

Java Code:
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.

Java Code:
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.

Java Code:
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:
Name:  swtevents.PNG
Views: 326
Size:  7.8 KB

Submit "SWT Events" to Facebook Submit "SWT Events" to Digg Submit "SWT Events" to del.icio.us Submit "SWT Events" to StumbleUpon Submit "SWT Events" to Google

Categories
SWT / JFace

Comments