Using Images with Buttons

Buttons can use more than text to explain to the user the purpose of the button—images can also be used. Those of you familiar with Borland’s C++ development environment may recall the popularity of the OK button that displayed a green checkmark image. You can use images associated with buttons to achieve a similar effect in your SWT applications.

How do I do that?

You have already used images in association with ToolItem buttons in Chapter 4. The techniques are the same when using images with the Button class:

Note

There does not appear, at this time, to be a way to use both text labels and images on the same button—something you can, however, accomplish with ToolItems.

final Button b1 = new Button(s, SWT.PUSH);
b1.setBounds(100,55,75,50);
b1.setImage(new Image(d, "c:\\icons\\check.jpg"));

This code creates the button shown in Figure 6-7.

Push button with image

Figure 6-7. Push button with image

You can also use images with SWT.CHECK and SWT.RADIO button styles to good effect in the appropriate application. Regardless of button style, the code to create an image button is the same.

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.