Change fonts and colors
When most developers think of customization, they think of special widgets and custom painting. However, some of the most effective customizations in the appearance of an application are the simplest. You can easily change the fonts and colors of individual widgets to enhance usability. The example snippet in Listing 3 changes the font type and size of a label to make it more suitable as a section heading. Colors work in a similar manner, as shown in Listing 4 and Figure 3.
Listing 3. Change fonts and colors
Label label = new Label(parent, SWT. NONE);
label.setText("Foo");
label.setFont(JFaceResources.getFont\
Registry().get(JFaceResources.HEADER_FONT));
Color color = new Color(Display.getCurrent(), 200, 0, 0);
label.setForeground(color);
|
Figure 3. Label with a custom color
This may seem trivial at first, but it's quite powerful. Consider label colors with respect to validation. Listing 4 shows an example SWT form that contains a few text controls/labels.
Listing 4. A basic set of label text pairs
parent.setLayout(new GridLayout(2, true));
Label labelFirst = new Label(parent, SWT. NONE);
labelFirst.setText("First:");
Text textFirst = new Text(parent, SWT.BORDER);
Label labelLast = new Label(parent, SWT. NONE);
labelLast.setText("Last:");
Text textLast = new Text(parent, SWT.BORDER);
|
As currently written, it's difficult for the user to determine which fields are invalid at what time. Adding color can make all the difference. Modify the method as shown below.
Listing 5. Modify label color on text change
parent.setLayout(new GridLayout(2, true));
final Label labelFirst = new Label(parent, SWT. NONE);
labelFirst.setText("First:");
originalColor = labelFirst.getForeground();
final Text textFirst = new Text(parent, SWT.BORDER);
textFirst.addModifyListener(new ModifyListener(){
public void modifyText(ModifyEvent e) {
if (textFirst.getText().length() > 5) {
labelFirst.setForeground(validationColor);
} else {
labelFirst.setForeground(originalColor);
}
textFirst.redraw();
}});
Label labelLast = new Label(parent, SWT. NONE);
labelLast.setText("Last:");
Text textLast = new Text(parent, SWT.BORDER);
|
Next, right-click the class and select Run as an SWT application. Type more than five letters into the first text widget and watch the change from black to red when the text is invalid, as shown in Figure 4. This form is now far more user-friendly.
Figure 4. Label color change on text change
A discussion of colors and fonts in SWT isn't complete without mentioning how the API handles them as resources. SWT is designed with its fonts and colors allocating operating system resources. As a result, these resources must be disposed when the application is finished using them to avoid application leaks. If you create a resource, dispose of it. If you use an existing resource, let the owner of that resource handle disposing it. In the previous example, you can add a dispose listener to the overall parent container, which disposes of any color resources.
Comparatively, however, this would not be correct because each label might have a font supplied internally instead of by the developer after creation. Disposing of such a resource would leave the control in an incorrect state.




