Previous | Next | Trail Map | Creating a User Interface | Using Components, the GUI Building Blocks


General Rules for Using Components

[CHANGE PAGE NAME? It's parallel to what's in other lessons, but the content of this page doesn't fit the name, and I don't think I want to make it fit....]

This page has general information on what components have in common. It tells you what functionality components inherit from the Component class. It also tells you how you can change the look and feel of components.

What the Component Class Provides

All components except for menus are implemented as subclasses of the Component(in the API reference documentation) class. From Component, they inherit a huge amount of functionality. By now, you should already know that the Component class provides the basis for all drawing and event handling. [CHECK -- we do say this explicitly, don't we, in the overview?] Here's a more complete list of the functionality Component provides:
Basic drawing support.
Component provides the paint(), update(), and repaint() methods, which enable Components to draw themselves onscreen. [Link to more info.]
Event handling.
Component defines the general-purpose handleEvent() method and a group of convenience methods that handle specific event types. The Component implementation of handleEvent() simply calls the convenience methods, which do nothing. Component also has methods for setting and getting the keyboard focus, enabling keyboard control of components. See [somewhere] for information. [I NEED TO COVER FOCUS SOMEWHERE. WHERE?]
Appearance control: font.
Component provides methods to get and set the current font, and to get information about the current font. See [Working with Text] for information.
Appearance control: color.
Component provides methods to get and set the foreground color and background color. The foreground color is the color used for all text in the component, as well as for any custom drawing the component performs. The background color is the color behind the text, which should provide contrast to the foreground color, for readability.
Image handling.
Component provides an implementation of the ImageObserver interface and defines methods to help Components display images. Note that most Components can't display images, since their appearance is implemented in platform-specific code. Canvases and most Containers can display images. See [somewhere] for information on working with images.
Onscreen size and position control.
All component sizes and positions (except for those of Windows) are subject to the whims of layout managers. Nonetheless, every component has at least some say in its size, if not its position. Component provides some methods that resize and position components and others that allow components to report their preferred and minimum sizes. It also provides methods that return information about the component's current size and location.

How to Change the Appearance and Behavior of Components.

The appearance of most components is platform-specific. Buttons look different on Motif systems than on Macintosh systems, for example. [Show screendumps of both.] Except for the appearance-affecting methods and variables provided by the component's class and superclasses, you can't easily change the appearance of components. Specifically, you can't change their appearance by creating a subclass of the component's class. To change a component's appearance, you must implement a Canvas subclass that has the look you want but the same behavior users expect from the component.

Although you can't easily make a major change to a component's appearance, you can change component behavior. For example, if only numeric values are valid in a text field, you might implement a TextField subclass that examines all keyboard events, intercepting any that aren't valid. This is possible because the platform-independent Component gets to see raw events before its platform-dependent peer does.

Note: Currently, components can intercept keyboard events but not mouse events. We plan to fix this in a future release.


Previous | Next | Trail Map | Creating a User Interface | Using Components, the GUI Building Blocks