AWT Control Fundamentals
The AWT supports the following types of controls:
Labels
Push buttons
Check boxes
Choice lists
Lists
Scroll bars
Text Editing
These controls are subclasses of Component. Although this is not a particularly rich set of
controls, it is sufficient for simple applications. (Note that both Swing and
JavaFX provide a substantially larger, more sophisticated set of controls.)
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 compRef)
Here, compRef is a
reference to an instance of the control that you want to add. A reference to
the object 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. Here is one of its forms:
void remove(Component compRef)
Here, compRef 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 other 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 24, 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 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).
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.
Related Topics
Privacy Policy, Terms and Conditions, DMCA Policy and Compliant
Copyright © 2018-2023 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.