devxlogo

Use SWT Listener Classes Effectively for Responsive Java UIs

Use SWT Listener Classes Effectively for Responsive Java UIs

nderstanding the SWT listener classes and their usage is essential to building complex, highly responsive user interfaces for Java applications. The listener classes in SWT are classified into two types:

  • Untyped listener API—the framework to write your own listener classes.
  • Typed listener API—the internal implementation of SWT.

The SWT Control class, the abstract superclass of all windowed user interface classes, defines all the add/remove listener methods but it does not implement them. The subclasses provide the actual implementation of these methods. For example, SWT allows you to add any listener to the Label widget but doesn’t generate any events when a user clicks the Label, because Label does not define any functionality for these methods. So it is up to the developer to know which listeners work with each widget.

Thankfully, SWT provides a flexible way to manage resources by enabling the removal of unnecessary listeners from the widgets. This is particularly useful for developing SWT applications on memory-constrained, small-footprint applications such as wireless connectivity applications.

This article introduces (with simple examples) the important listener classes, emphasizing those that are new to SWT (not provided in Swing). It concludes a three-article series on SWT development that began with “SWT Creates Fast, Native-looking GUIs for Your Java Apps” and “You Don’t Have to Swing to Make Great Java UIs“.

Untyped Listener API
The SWT untyped listener API provides three important classes that you can use to write your own listeners:

  • Event
    Instances of this class provide a description of a particular event that occurred within SWT. The SWT untyped listener API uses these instances for all event dispatching. Event provides fields like type, widget, time etc to register all the data about the event.
  • Listener
    Any class that implements this interface must define the handleEvent method. When an event that the receiver has registered for occurs, handleEvent is invoked.
  • Widget
    Any listener class can be added to a widget using the addListener (int eventType, Listener handler) method and removed using the removeListener (int eventType, Listener handler) method. When the specified event occurs, handleEvent(...) is sent to the listener instance.

In the example code, the shell implements a custom mouse listener to capture the MouseUp, MouseDown, and MouseMove events.

Typed Listener API
SWT provides 16 different listener classes under the typed listener API.

Mouse-based Listeners
Mouse-based listeners capture the events generated by the mouse such as mouse-clicks and mouse moves.

MouseListener
MouseListener deals with the events generated when users press mouse buttons. When the user presses or releases a mouse button, it invokes the appropriate method:

	void mouseDoubleClick(MouseEvent me)	void mouseDown(MouseEvent me)	void mouseUp(MouseEvent me) 

MouseAdapter provides the basic definition of these methods.

MouseMoveListener
MouseMoveListener deals with the events generated as the user moves the mouse pointer. As the mouse moves, it invokes the mouseMove method:

	void mouseMove(MouseEvent me)

MouseTrackListener
MouseTrackListener deals with the events generated as the user passes the mouse pointer (or hovers) over controls. This listener is used to display tool tip text messages:

	void mouseEnter(MouseEvent me)	void mouseExit(MouseEvent me)	void mouseHover(MouseEvent me)

MouseTrackAdapter defines these methods, and developers can overwrite the definition of any method.

Text Listeners
Text listeners capture the events generated when a user modifies or is about to modify the text inside a text-widget..

VerifyListener
VerifyListener deals with the events generated when a user is about to modify text. It fires the event when the user is about to modify text in a text widget:

txt.addVerifyListener(new VerifyListener(){	public void verifyText(VerifyEvent ve){			.......		       	}});

Swing has no equivalent component that offers the same functionality.

ModifyListener
ModifyListener deals with the events generated when a user modifies text. It invokes the modifyText() method when the user modifies text. In the sample code, when you type text in the text widget you first see a Verify event message followed by a Modify event message.

Menu and MenuItem Listeners
Menu and MenuItem listeners deal with the events generated when a user activates or clicks the Menu or MenuItem widget.

ArmListener
ArmListener is a new listener functionality provided by SWT. It deals with the events generated when a user highlights a MenuItem with the mouse or keyboard but has not yet selected it. The widgetArmed(ArmEvent ae) is invoked when a widget is armed, or “about to be selected”.

MenuListener
MenuListener deals with hiding and showing menus. When a menu is hidden or shown, the menuHidden() or menuShown() method is invoked, respectively.

HelpListener
SWT provides a direct association between the standard F1 key and the Help menu. HelpListener can be added to any control (or to any widget, as widgets are sub classes of control). In the sample code, the help listener is added to the shell to bring out help for the screen. This is a very helpful feature for complex UI screens.

Shell and Composite Listeners
Shell and composite listeners deal with the events generated when a user invokes or activates the Shell or Composite.

ShellListener
ShellListener is very helpful for managing resource allocation. Based on the state of a shell’s changes, the appropriate method [shellActivated() or shellClosed() or shellDeactivated() or shellDeiconified() or shellIconified()] is invoked.

ControlListener
This type of listener does not exist in Swing. ControlListener deals with the events generated when a user moves and resizes controls. When the user moves and resizes a control, it invokes the controlMoved() or controlResized() method.

DisposeListener
When a widget is disposed, the widgetDisposed() method is invoked.

PaintListener
This type of listener does not exist in Swing. PaintListener deals with the events generated when the control needs painting. However, the paintControl method is invoked after a paint event occurs.

TraverseListener
When a traverse event occurs in a control, the keyTraversed method is invoked. Traversal keys are typically tab and arrow keys, along with certain other keys on some platforms. The feature enables users to traverse the control on an application by simply typing the traverse keys rather than positioning the mouse pointer on the control and clicking. Swing has no equivalent to this class.

The Rest Is Up to You
By using the SWT listener classes effectively as prescribed in this article, you can win the hearts of the application users with highly responsive UIs.

devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist