img
· Scroll bars
· Text editing
These controls are subclasses of Component.
Adding and Removing Controls
To include a control in a window, you must add it to the window. To do this, you must first
create an instance of the desired control and then add it to a window by calling add( ), which
is defined by Container. The add( ) method has several forms. The following form is the
one that is used for the first part of this chapter:
Component add(Component compObj)
Here, compObj is an instance of the control that you want to add. A reference to compObj is
returned. Once a control has been added, it will automatically be visible whenever its parent
window is displayed.
Sometimes you will want to remove a control from a window when the control is no
longer needed. To do this, call remove( ). This method is also defined by Container. It has
this general form:
void remove(Component obj)
Here, obj is a reference to the control you want to remove. You can remove all controls by
calling removeAll( ).
Responding to Controls
Except for labels, which are passive, all controls generate events when they are accessed by
the user. For example, when the user clicks on a push button, an event is sent that identifies
the push button. In general, your program simply implements the appropriate interface and
then registers an event listener for each control that you need to monitor. As explained in
Chapter 22, once a listener has been installed, events are automatically sent to it. In the
sections that follow, the appropriate interface for each control is specified.
The HeadlessException
Most of the AWT controls described in this chapter now have constructors that can throw
a HeadlessException when an attempt is made to instantiate a GUI component in a
non-interactive environment (such as one in which no display, mouse, or keyboard is
present). The HeadlessException was added by Java 1.4. You can use this exception to
write code that can adapt to non-interactive environments. (Of course, this is not always
possible.) This exception is not handled by the programs in this chapter because an
interactive environment is required to demonstrate the AWT controls.
Labels
The easiest control to use is a label. A label is an object of type Label, and it contains a string,
which it displays. Labels are passive controls that do not support any interaction with the
user. Label defines the following constructors:
Search WWH :
Custom Search
Previous Page
Java SE 6 Topic Index
Next Page
Java SE 6 Bookmarks
Home