Responding to controls || Using AWT controls, Layout Managers, and Menus || Bcis Notes

Responding to controls || Using AWT controls, Layout Managers, and Menus || Bcis Notes

Responding to Controls

In the process of responding to controls except for labels, which are passive controls, 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, our program simply implements the appropriate interface and then registers an event listener for each control that we need to monitor.

  • Labels
  • Pushbuttons
  • Checkboxes
  • Choice lists
  • Lists
  • Scroll bars
  • Text editing


The easiest control to use a label A Allie is an object of type Label, and it contains a string, which it displays. tables are passive controls that do not support any interaction with the user. “Label defines the following constructors:
label( )
Label(String st;’)
Labe1(String str, int how)
The first version creates a blank label. The second version creates a label that contains the string specified This string is left-justified. The third version creates a label that contains the string specified by the alignment specified by how. The value of hoto must be one of these constants: Label.LEFT, Label.right, or Label.CENTER.

Using Buttons

The most widely used control is the push button, A push button is a component that contains a label and that generates an event when it is pressed. Push buttons arc objects of type’ Button. A button defines these two constructors:
Button(String str)
The first version creates an empty button. The second creates a button that contains str
as a label. ‘
After a button has been created, you can set its label by calling set Label(). You can
retrieve its label by calling getf.abelt ), These methods are as follows: ‘
void set Label(String str)
String get Label( )
Here, it becomes the’ new label for the button.

Handling Buttons

Each time a button is pressed, an action event is generated. sent to any listeners .that previously registered an interest in receiving action event notifications from that component. Each listener implements the Action Listener interface. The interface defines the action Performed( ) method, which is called when an event occurs. An Adion Event I object is supplied as the argument to this method. It contains both a reference to the button that generated the event and a reference to the string that is the label of the button. Usually, either value may be used to identify the button, as you will see. Here is an example that creates three buttons labeled “Yes;” “No,” and “Undecided.” Each time one is pressed, a message is displayed that reports which button has been.pressed. In this version, the label of the button is used to determine which button has been pressed: The label is obtained by calling the get Action.

Check Boxes

A checkbox is a control that is used to turn an option on or off. It consists of a small box that can either contain a checkmark or not. There is a label associated with each checkbox that describes what option the box represents. You change the state of a checkbox by clicking on it. Checkboxes can be used individually or as part of a group. Checkboxes are objects of the Checkbox class.

Checkbox supports these constructors:                                                                                                                                  Checkbox( )
Checkbox(String str)
Checkbox(String str, boolean on)
Checkbox(String str, boolean on, CheckboxGroup cbGroup)
Checkbox(String str, CheckboxGroup cbGroup, boolean on)


It is possible to create a set of mutually exclusive checkboxes in which one and only one checkbox in the group can be checked at any one time. These checkboxes are often called radio buttons because they act as the station selector on a car radio—only one station can be selected at any one time. To create a set of mutually exclusive checkboxes, you must first define the group to which they will belong and then specify that group when you construct the checkboxes. Checkbox groups are objects of type CheckboxGroup. Only the default constructor is defined, which creates an empty group. You can determine which checkbox in a group is currently selected by calling get selectedCheckbox( ). You can set a checkbox by calling setSelectedCheckbox( ).                                                                                                                        These methods are as follows:                                                                                                                                              Checkbox get selectedCheckbox( )
void setSelectedCheckbox(Checkbox which)


The List class provides a compact, multiple-choice, scrolling selection list. Unlike the Choice object, which shows only the single selected item in the menu, a List object can be constructed to show any number of choices in the visible window. It can also be created to allow multiple selections.                                                                                       The list provides these constructors:
List( )
List(int numRows)
List(int numRows, boolean multiple select)

Handling Lists

To process list events, you will need to implement the ActionListener interface. Each time a List item is double-clicked, an ActionEvent object is generated. Its getActionCommand( ) method can be used to retrieve the name of the newly selected item. Also, each time an item is selected or deselected with a single click, an ItemEvent object is generated. Its get state change( ) method can be used to determine whether a selection or deselection triggered this event. getItemSelectable( ) returns a reference to the object that triggered this event.

Scroll Bars

Scroll bars are used to select continuous values between a specified minimum and maximum. Scroll bars may be oriented horizontally or vertically. A scroll bar is actually a composite of several individual parts. Each end has an arrow that you can click to move the current value of the scroll bar one unit in the direction of the arrow. The current value of the scroll bar relative to its minimum and maximum values are indicated by the slider box (or thumb) for the scroll bar. The slider box can be dragged by the user to a new position. The scroll bar will then reflect this value. In the background space on either side of the thumb, the user can click to cause the thumb to jump in that direction by some increment larger than 1. This action translates into some form of the page up and pages down.

Scroll bars are encapsulated by the Scrollbar class.

Scrollbar defines the following constructors:
Scrollbar( )
Scrollbar(int style)
Scrollbar(int style, int initialValue, int thumb size, int min, int max)


The TextField class implements a single-line text-entry area, usually called an edit control. Text fields allow the user to enter strings and to edit the text using the arrow keys, cut and paste keys, and mouse selections. TextField is a subclass of TextComponent.                                                                                                                                                      TextField defines the following constructors:
TextField( )
TextField(int numChars)
TextField(String str)
TextField(String str, int numChars)


Sometimes a single line of text input is not enough for a given task. To handle these situations, the AWT includes a simple multiline editor called TextArea.
Following are the constructors for TextArea:
TextArea( )
TextArea(int numLines, int numChars)
TextArea(String str)
TextArea(String str, int numLines, int numChars)
TextArea(String str, int numLines, int numChars, int sBars)

You may also like Adding and removing controls

Be the first to comment

Leave a Reply

Your email address will not be published.