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 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 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 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 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 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 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 deals with hiding and showing menus. When a menu is hidden or shown, the menuHidden() or menuShown() method is invoked, respectively.

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 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.

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.

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

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.

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.

Share the Post:
XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes

ransomware cyber attack

Why Is Ransomware Such a Major Threat?

One of the most significant cyber threats faced by modern organizations is a ransomware attack. Ransomware attacks have grown in both sophistication and frequency over the past few years, forcing

data dictionary

Tools You Need to Make a Data Dictionary

Data dictionaries are crucial for organizations of all sizes that deal with large amounts of data. they are centralized repositories of all the data in organizations, including metadata such as