Skip to main content

By clicking Submit, you agree to the developerWorks terms of use.

The first time you sign into developerWorks, a profile is created for you. Select information in your profile (name, country/region, and company) is displayed to the public and will accompany any content you post. You may update your IBM account at any time.

All information submitted is secure.

  • Close [x]

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerworks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

By clicking Submit, you agree to the developerWorks terms of use.

All information submitted is secure.

  • Close [x]

Customizing Eclipse RCP applications

Techniques to use with SWT and JFace

Scott Delap (scott@clientjava.com), Desktop/Enterprise Java Consultant
Scott Delap is president of Rich Client Solutions Inc., a software consulting firm focusing on technologies such as Swing, Eclipse RCP, GWT, Flex, and Open Laszlo. He is actively involved in the Java community, speaking at events such as NFJS, QCon and JavaOne. He is also the Java editor of InfoQ.com and runs ClientJava.com, a portal focused on desktop Java development.
Annas Andy Maleh (andy@obtiva.com), Consultant, MichaelDKelly.com
Annas "Andy" Maleh is a consultant at Obtiva Corp., a firm that specializes in Eclipse RCP development, Ruby on Rails development and training, and helping teams transition to Agile methodologies. He is currently involved with an Eclipse RCP project to build a custom CRM application for an international corporation. He works on a team that follows eXtreme Programming practices, programs professionally in Java and Ruby, and participates in work relating to UI design enhancement. At EclipseWorld 2006, he gave two presentations relating to Eclipse RCP development. He is a Sun Certified Java Programmer who holds a bachelor's degree in computer science from McGill University.

Summary:  Most developers think that an Eclipse Rich Client Platform (RCP) application must look similar in nature to the Eclipse integrated development environment (IDE). This isn't the case, however. This tutorial will explain a number of simple techniques you can use with the Standard Widget Toolkit (SWT) and JFace to create applications that have much more personality than the Eclipse IDE.

Date:  27 Feb 2007
Level:  Intermediate PDF:  A4 and Letter (1058 KB | 40 pages)Get Adobe® Reader®

Activity:  40416 views
Comments:  

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
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
Label color change on text change

Operating system resources

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.

5 of 18 | Previous | Next

Comments



static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=Open source
ArticleID=197254
TutorialTitle=Customizing Eclipse RCP applications
publish-date=02272007
author1-email=scott@clientjava.com
author1-email-cc=
author2-email=andy@obtiva.com
author2-email-cc=