Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

The effective user interface

Five ways to enhance the appearance and effectiveness of your user interface

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone

Introduction

In order to be effective, a user interface must do much more than throw a panel full of buttons, labels, and scrollbars onto the screen. Instead, it must "look right" and it must "act right." Unfortunately, there is often a lack of consensus as to what constitutes "right." It is almost certain that someone else's definition of "right" will sooner or later diverge from yours.

The designers of the Abstract Windowing Toolkit (or AWT) attempted to make the classes provided by the AWT "do the right thing." Much of the time, the result is pleasing and effective. However, they were wise enough to provide mechanisms by which the AWT's idea of the "right" behavior and appearance could be changed, so that programmers could alter the behavior and appearance of components.

This column, my last on the AWT, introduces five techniques for enhancing a user interface. The techniques draw upon and extend the concepts learned in my previous two columns. I will describe how to select the font, the background color, and the foreground color of components; how to specify the preferred size of a component, and the insets of a container; and how to enable and disable components on the fly.

Setting the Font

The font with which component text is displayed contributes to the impact of a user interface. An effective user interface shouldn't be littered with fonts of all styles and sizes, but it may use two or three fonts to enhance its attractiveness and expressiveness. The applet in Figure 1 illustrates this point. It uses three fonts (in three sizes and styles) to draw the user's attention to successive portions of the user interface. The source is available here.



Figure 1: Fonts in use

The default font for components is the "Dialog" font. Java provides a number of other fonts for specialized display purposes. The exact number depends on the platform. Figure 2 contains a listing of the fonts available in your implementation of Java. The source is available here.



Figure 2: Fonts available on your system

When a programmer needs to display a component, such as a TextArea object, in a font other than the default font, the new font may be selected with the setFont() method:

public void setFont(Font f)


The setFont() method expects a Font object as a parameter. The code in Listing 1 illustrates the use of the setFont() method.

TextArea ta = new TextArea();
Font f = new Font("Helvetica", Font.ITALIC, 12);
ta.setFont(f);


Listing 1: The setFont() method

The code in Listing 1, with only slight modifications, will work for any component.

Figure 3 illustrates the result of changing the font for several components provided by the AWT. The font in this example has been set to 12 point, italic Helvetica.



Figure 3: The result of changing the font

If a container's font is changed, all of the components placed within the container will automatically use the new font. The code in Listing 2 illustrates this point. The font is set only for the Frame object. The Button, Checkbox, and TextArea objects, in turn, also use that font.

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
Comment
Login
Forgot your account info?
Add comment
Anonymous comments subject to approval. Register here for member benefits.
Have a JavaWorld account? Log in here. Register now for a free account.
Resources