AWT Controls in Java
AWT Controls in Java
In this article, I am going to discuss AWT Controls in Java with Examples. Please read our previous article, where
we discussed Abstract Windows Toolkit (AWT) in Java. At the end of this article, you will understand the following
pointers in detail which are related to AWT Controls in java with Examples.
2. Labels
3. Buttons
4. Canvas
5. Checkbox
6. Radio Buttons
8. List Control
Controls are components that allow a user to interact with your application in various ways. The AWT supports the
following types of controls:
Labels
The easiest control to use is a label. A label contains a string and is an object of type Label. Labels are passive
Label Constructors:
Label Methods:
1. void setText(String str): It is used to set or change the text in a label by using the setText() method. Here,
str specifies the new label.
2. String getText(): It is used to obtain the current label by calling getText() method. Here, the current label is
returned.
3. void setAlignment(int how): It is used to set the alignment of the string within the label by calling
setAlignment() method. Here, how is one of the alignment constants?
4. int getAlignment(): It is used to obtain the current alignment, getAlignment() is called.
import java.awt.*;
import java.applet.*;
/*
<applet code = "LabelDemo" width=300 height=200>
</applet>
*/
public class LabelDemo extends Applet
{
public void init ()
{
Label one = new Label ("One");
Label two = new Label ("Two");
Label three = new Label ("Three");
add (one);
add (two);
add (three);
}
}
Output:
The most widely used control is Button. A button is a component that contains a label and that generates an event
when it is pressed.
Button Constructors:
Button Methods :
1. void setLabel(String str): You can set its label by calling setLabel(). Here, str is the new Label for the
button.
2. String getLabel(): You can retrieve its label by calling getLabel() method.
Example to understand AWT Button Control in Java:
import java.awt.*;
import java.awt.event.*;
Output:
AWR Canvas Control in java
Canvas encapsulates a blank window upon which you can draw in an application or receive inputs created by the
user.
Canvas Constructor:
Canvas Methods:
import java.awt.*;
import java.awt.event.*;
public CanvasDemo ()
{
prepareGUI ();
}
mainFrame.add (headerLabel);
mainFrame.add (controlPanel);
mainFrame.add (statusLabel);
mainFrame.setVisible (true);
}
Output:
A checkbox may be a control that’s used to turn an option on or off. It consists of a little box that will either contain a
check or not. There’s a label related to each checkbox that describes what option the box represents. You modify
the state of a checkbox by clicking on. Checkboxes are often used individually or as a part of a gaggle. Checkboxes
are objects of the Checkbox class.
Creating Checkbox : Checkbox cb = new Checkbox(Label);
Checkbox Constructor
1. Checkbox() throws HeadlessException: Creates a checkbox whose label is initially blank. The state of the
checkbox is unchecked.
2. Checkbox(String str) throws HeadlessException: Creates a checkbox whose label is specified by str. The
state of the checkbox is unchecked.
3. Checkbox(String str, Boolean on) throws HeadlessException: It allows you to line the initial state of the
checkbox. If one is true, the checkbox is initially checked; otherwise, it’s cleared.
4. Checkbox(String str, Boolean on, CheckboxGroup cbGroup) throws HeadlessException or
Checkbox(String str, CheckboxGroup cbGroup, Boolean on) throws HeadlessException: It creates a
checkbox whose label is specified by str and whose group is specified by cbGroup. If this checkbox isn’t a
part of a gaggle, then cbGroup must be null. the worth of on determines the initial state of the checkbox.
Methods of Checkbox
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
It is possible to make a group of mutually exclusive checkboxes during which one and just one checkbox up the
group are often checked at anybody time. These checkboxes are often called radio buttons because they act just
like the station selector on a car radio, only one station is often selected at anybody’s time. To create a group of
mutually exclusive checkboxes, you want to first define the group to which they’re going to belong then specify that
group once you construct the checkboxes. Checkbox groups are objects of the type CheckboxGroup. Only the
default constructor is defined, which creates an empty group.
Creating Radiobutton :
CheckboxGroup cbg = new CheckboxGroup();
Checkbox rb = new Checkbox(Label, cbg, boolean);
CheckboxGroup Methods
1. Checkbox getSelectedCheckbox(): You can determine which checkbox in a group is currently selected by
calling getSelectedCheckbox().
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/* <applet code="CBGroup" width=250 height=200> </applet> */
Output:
AWT Choice Control in Java
This component will display a group of times as a drop-down menu from which a user can select only one item. The
choice component is used to create a pop-up list of items from which the user may choose. Therefore, Choice
control is a form of a menu. When it is inactive, a Choice component takes up only enough space to show the
currently selected item. When the user clicks on a Choice component, the whole list of choices pops up, and a new
selection can be made.
Note: Choice only defines the default constructor, which creates an empty list.
Choice Methods
1. void add(String name): To add a selection to the list, use add(). Here, the name is the name of the item
being added.
2. String getSelectedItem(): It determines which item is currently selected. It returns a string containing the
name of the item.
3. int getSelectedIndex(): It determines which item is currently selected. It returns the index of the item.
4. int getItemCount(): It obtains the number of items in the list.
5. void select(int index): It is used to set the currently selected item with a zero-based integer index.
6. void select(String name): It is used to set the currently selected item with a string that will match a name in
the list.
7. String getItem(int index): It is used to obtain the name associated with the item at the given index. Here,
the index specifies the index of the desired items.
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/* <applet code="ChoiceDemo" width=300 height=180> </applet> */
Output:
This component will display a group of items as a drop-down menu from which a user can select only one item. The
List class provides a compact, multiple-choice, scrolling selection list. Unlike the selection object, which shows only
the only selected item within the menu, an inventory object is often constructed to point out any number of choices
within the visible window. It also can be created to permit multiple selections.
1. List() throws HeadlessException: It creates a list control that allows only one item to be selected at any
one time.
2. List(int numRows) throws HeadlessException: Here, the value of numRows specifies the number of
entries in the list that will always be visible.
3. List(int numRows, boolean multipleSelect) throws HeadlessException: If multipleSelect is true, then the
user may select two or more items at a time. If it’s false, then just one item could also be selected.
Method of Lists
1. void add(String name): To add a selection to the list, use add(). Here, the name is the name of the item
being added. It adds items to the end of the list.
2. void add(String name, int index): It also adds items to the list but it adds the items at the index specified by
the index.
3. String getSelectedItem(): It determines which item is currently selected. It returns a string containing the
name of the item. If more than one item is selected, or if no selection has been made yet, null is returned.
4. int getSelectedIndex(): It determines which item is currently selected. It returns the index of the item. The
first item is at index 0. If more than one item is selected, or if no selection has yet been made, -1 is returned.
5. String[] getSelectedItems(): It allows multiple selections. It returns an array containing the names of the
currently selected items.
6. int[] getSelectedIndexes(): It also allows multiple selections. It returns an array containing the indexes of
the currently selected items.
7. int getItemCount(): It obtains the number of items in the list.
8. void select(int index): It is used to set the currently selected item with a zero-based integer index.
9. String getItem(int index): It is used to obtain the name associated with the item at the given index. Here,
the index specifies the index of the desired items.
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/* <applet code="ListDemo" width=300 height=180> </applet> */
Scrollbars are used to select continuous values between a specified minimum and maximum. Scroll bars may be
oriented horizontally or vertically. A scroll bar is really a composite of several individual parts. Each end has an
arrow that you simply can click to get the present value of the scroll bar one unit within 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 for
the scroll bar. Scrollbars are encapsulated by the Scrollbar class.
Scrollbar Constructor
the initial value of the scroll bar is passed in initialValue. The number of units represented by the peak of the
thumb is passed in thumbSize. The minimum and maximum values for the scroll bar are specified by min and
max.
Scrollbar Methods
1. void setValues(int initialValue, int thumbSize, int min, int max): It is used to set the parameters of the
constructors.
2. int getValue(): It is used to obtain the current value of the scroll bar. It returns the current setting.
3. void setValue(int newValue): It is used to set the current value. Here, newValue specifies the new value for
the scroll bar. When you set a worth, the slider box inside the scroll bar is going to be positioned to reflect the
new value.
4. int getMaximum(): It is used to retrieve the minimum values. They return the requested quantity. By default,
1 is the increment added to the scroll bar.
5. int getMaximun(): It is used to retrieve the maximum value. By default, 1 is the increment subtracted from
the scroll bar.
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/* <applet code="SBDemo" width=300 height=200> </applet> */
Output
The TextComponent class is the superclass of any component that permits the editing of some text. A text
component embodies a string of text. The TextComponent class defines a group of methods that determine whether
or not this text is editable. There are two types of TextComponent:
1. TextField
2. TextArea
The TextField component will allow the user to enter some text. It is used to implement a single-line text-entry area,
usually called an edit control. It also allows the user to enter strings and edit the text using the arrow keys, cut and
paste keys, and mouse selections. TextField is a subclass of TextComponent.
TextField Constructors
TextField Methods
1. String getText(): It is used to obtain the string currently contained in the text field.
2. void setText(String str): It is used to set the text. Here, str is the new String.
3. void select(int startIndex, int endIndex): It is used to select a portion of the text under program control. It
selects the characters beginning at startIndex and ending at endIndex-1.
4. String getSelectedText(): It returns the currently selected text.
5. boolean isEditable(): It is used to determine editability. It returns true if the text may be changed and false if
not.
6. void setEditable(boolean canEdit): It is used to control whether the contents of a text field may be modified
by the user. If canEdit is true, the text may be changed. If it is false, the text cannot be altered.
7. void setEchoChar(char ch): It is used to disable the echoing of the characters as they are typed. This
method specifies a single character that TextField will display when characters are entered.
8. boolean echoCharIsSet(): By this method, you can check a text field to see if it is in this mode.
9. char getEchochar(): It is used to retrieve the echo character.
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*
<applet code="TextFieldDemo" width=380 height=150> </applet>
*/
Output
Here, you will get a response when the user will press ENTER.
Sometimes one line of text input isn’t enough for a given task. To handle these situations, the AWT includes an easy
multiline editor called TextArea.
TextArea Constructor
TextArea Methods
select( ), isEditable( ), and setEditable( ) methods described in the TextField section. TextArea adds the following
methods:
1. void append(String str): It appends the string specified by str to the end of the current text.
2. void insert(String str, int index): It inserts the string passed in str at the specified index.
3. voidReplaceRange(String str, int startIndex, int endIndex): It is used to replace the text. It replaces the
characters from startIndex to endIndex-1.
import java.awt.*;
import java.applet.*; /* <applet code="TextAreaDemo" width=300
height=250> </applet> */
Output