Chapter 14. Other SWT Listeners

In many of the examples so far you have seen that to respond to user events such as button clicking, you develop code using a listener. Until now, these listeners have been specific to the widget to which they were attached (i.e., attaching a SelectionListener to a button to respond to the click event). The SWT provides other Listener classes that are useful in making our user interfaces respond to other types of user actions. To understand event types, you must begin with the proposition that for every action a user can take in your application, an event will be generated by the operating system. This means that each time the user moves the mouse, or presses a mouse button or a key on the keyboard, an event, or multiple events, will occur.

Most of these events go unhandled by your application. After all, for most applications do you really need to know that the user moved the mouse from point A to point B within a window? Probably not. You need only know whether the user moved the mouse into a critical area or whether the user pressed a mouse button while pointing at a widget.

For those events which you do need to capture and take action on, you must add a Listener to your code. A Listener must be attached to some user interface element—the Shell, MenuItem, or Composite, or one of the widgets placed in one of the container-type classes. There is no such thing as a “free-standing” Listener.

Earlier, you were exposed to how to add the most common listeners ...

Get SWT: A Developer's Notebook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.