Creating Toggle Buttons

SWT.TOGGLE-style buttons look like push buttons, but behave like checkbox buttons in that they stay pushed when clicked. This enables you to create buttons that provide the user with the same look as SWT.PUSH buttons, but provide them with a visual indication that the button has been clicked.

How do I do that?

Creating an SWT.TOGGLE button is, once again, just a matter of specifying the correct style:

final Button toggle = new Button(s, SWT.TOGGLE);

A button with the SWT.TOGGLE style appears as shown in Figure 6-4 until the button is clicked, when it appears as shown in Figure 6-5. Clicking the button again will return it to the Figure 6-4 state.

Untoggled button

Figure 6-4. Untoggled button

A toggled button

Figure 6-5. A toggled button

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.