AWT Controls
Adding and Removing Controls
• In order to include a control in a window, we must add it
to the window.
• So, we must first create an instance of the desired control
and then add it to a window by calling add( ), which is
defined by Container.
• add(Component compObj)
• Here, compObj is an instance of the control that we 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 we want to remove a control from a window
when the control is no longer needed. For doing this, call
remove( ).
• This method is also defined by Container. It has this
general form:
• remove(Component obj)
• We can remove all controls by calling removeAll( )
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.
• the AWT supports the following types of controls:
– Labels
– Buttons
– Check boxes
– choice
– Lists
– Scroll Bar
– Textfield
– Text area etc.
1. Labels:
• A label is an object of type Label, and it contains a string ,
which it displays.
• Label defines the following constructors
– Label()
– Label(String str)
– Label(String str, int how)
• The first version creates a blank label.
• The second version creates a label that contains the string
specified by str. This string is left-justified.
• The third version creates a label that contains the string
specified by str using the alignment specified by how.
• The value of how must be one of these 3 constants:
• [Link] , [Link] , [Link].
Design a java applet that displays 3 labels Name, address and
phone number with its background color set as green and the text
color should be red.
import [Link].*;
[Link] (html file)
import [Link].*;
public class apps extends Applet <html>
{ <applet code=“[Link]”
public void init( ) width=“100”
{ height=“100’></applet>
</html>
Label l1 = new Label("Name");
Label l2 = new Label("Address");
Label l3 = new Label("Phone no:");
setBackground([Link]); OUTPUT
setForeground([Link]);
add(l1);
add(l2); Name
add(l3); Address
Phone no:
}
}
2. Buttons:
• A button is a component that contains a label and that
generates an event when it is pressed.
• Buttons are objects of type Button.
• Button defines two constructors:
– Button()
– Button(String str)
• The first version creates an empty button.
• The second creates a button that contains str as a label.
Example of a button click
import [Link].*;
import [Link].*;
public class ActionEventDemo1 extends Applet implements ActionListener
{
public void init()
{
Button b1 = new Button("Submit");
add(b1);
setLayout(new FlowLayout());
[Link](this);
}
public void actionPerformed(ActionEvent e)
{
[Link]("Button pressed");
}
}
Html file
<html>
<applet code=“[Link]” width=
“100” height=“100” > </applet>
</html>
• Implementing an interface means supplying methods with the
right signatures.
• To implement the ActionListener interface, the listener class
must have a method called actionPerformed that receives an
ActionEvent object as a parameter.
• Whenever the user clicks the button, the Button object creates
an ActionEvent object and calls
[Link](event), passing that event object.
output
3. Check boxes:
• A check box is a control that is used to turn an option on
or off. It consists of a small box that can either contain a
check mark or not.
• There is a label associated with each check box that
describes what option the box represents.
• You can change the state of a check box by clicking on
it.
• Check boxes are objects of the Checkbox class.
[Link] (html file)
import [Link].*;
import [Link].*; <html>
public class apps extends Applet <applet code=“[Link]”
width=“100”
{ height=“100’></applet>
public void init( ) </html>
{
Label l1 = new Label("Name");
Label l2 = new Label("Address");
Label l3 = new Label("Phone no:");
Checkbox c1 = new Checkbox("Agree terms and conditions");
add(l1);
add(l2);
add(l3);
add(c1);
}
}
• Checkbox group:
• It is possible to create a set of mutually exclusive check
boxes in which one and only one check box in the group
can be checked at any one time.
• These check boxes are often called radio buttons, because
they act like the station selector on a car radio—only one
station can be selected at any one time.
• To create a set of mutually exclusive check boxes, you must
first define the group to which they will belong and then
specify that group when you construct the check boxes.
• Check box groups are objects of type CheckboxGroup.
Only the default constructor is defined, which creates an
empty group.
• You can determine which check box in a group is currently
selected by calling getSelectedCheckbox( )
import [Link].*;
import [Link].*;
import [Link].*;
public class CBGroup extends Applet implements ItemListener
{
String msg = "";
CheckboxGroup cbg;
public void init()
{
cbg = new CheckboxGroup();
Checkbox male = new Checkbox("Male", cbg, true);
Checkbox female= new Checkbox("Female", cbg, false);
add(male);
add(female);
[Link](this);
[Link](this);
}
public void itemStateChanged(ItemEvent ie)
{ [Link] (html file)
repaint(); <html>
<applet code=“[Link]”
} width=“100”
public void paint(Graphics g) height=“100’></applet>
</html>
{
msg += [Link]().getLabel();
[Link](msg, 6, 100);
msg=" ";
}
}
4. Choice control:
• The Choice class is used to create a pop -up list of items
from which the user may choose.
• Thus, a Choice control is a form of menu. When inactive,
a Choice component takes up only enough space to show
the currently selected item.
• When the user clicks on it, the whole list of choices pops
up, and a new selection can be made.
• Each item in the list is a string that appears as a left
justified label in the order it is added to the Choice
object.
import [Link].*;
import [Link].*;
import [Link].*;
public class ChoiceDemo extends Applet implements ItemListener
{
Choice co;
String msg = "";
public void init()
{
co = new Choice();
[Link] (html file)
[Link]("India");
[Link]("America"); <html>
[Link]("Africa"); <applet code=“ChoiceDemo”
[Link]("Australia"); width=“100”
height=“100’></applet>
add(co); </html>
[Link](this);
}
public void itemStateChanged(ItemEvent ie)
{
repaint();
}
public void paint(Graphics g)
{
msg += [Link]();
[Link](msg, 6, 100);
msg=" ";
}
}
5. Lists:
• 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.
• Constructors:
• List()
• List(int numRows)
• List(int numRows, boolean multipleselect)
• The first version creates a list control that allows only
one item to be selected at any one time.
• In the second form, the value of numRows specifies
the number of entries in the list that will always be
visible
• If multipleSelect is true, then the user may select two
or more items at a time.
• If it is false, then only one item may be selected.
• To add a selection to the list , call add().
[Link] (html file)
import [Link].*;
import [Link].*; <html>
<applet code=“[Link]”
public class ListDemo extends Applet width=“100”
{ height=“100’></applet>
</html>
public void init()
{
List os = new List(4, true);
[Link](“India");
[Link](“America");
[Link](“Africa");
[Link](“Australia");
add(os);
}
}
6. ScrollBar
• The object of Scrollbar class is used to add horizontal and
vertical scrollbar. Scrollbar is a GUI component allows
us to see invisible number of rows and columns.
• It consists of the following constructors
• Scrollbar()
• Scrollbar(int style)
• The first form creates a vertical scroll bar.
• The second form allow us to specify the orientation of the
scroll bar.
• If style is [Link], a vertical scroll bar is
created.
• If style is [Link], the scroll bar is
horizontal
import [Link].*;
public class ScrollBar
{
Frame frame;
Label label ;
ScrollBar()
{
frame = new Frame("Scrollbar");
label = new Label("Displaying a horizontal and verticial Scrollbar in the Frame");
Scrollbar scrollB1 = new Scrollbar([Link]);
Scrollbar scrollB2 = new Scrollbar([Link]);
[Link](scrollB1,[Link]);
[Link](scrollB2,[Link]);
[Link](370,270);
[Link](true);
}
}
public static void main(String... ar)
{
ScrollBar sb = new ScrollBar ();
7. TextField
Text field:
• 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.
TextArea:
• 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.
Example 1.
import [Link].*;
import [Link].*;
public class apps extends Applet [Link] (html file)
{ <html>
<applet code=“apps” width=“100”
public void init( ) height=“100’></applet>
</html>
{
TextField t1 = new TextField();
TextArea t2 = new TextArea();
add(t1);
add(t2);
}
}
Example 2.
import [Link].*;
import [Link].*;
public class TextFieldExample extends Frame implements ActionListener
{
TextField tf1,tf2,tf3;
Button b1,b2;
TextFieldExample()
{
tf1=new TextField();
[Link](50,50,150,20);
tf2=new TextField();
[Link](50,100,150,20);
tf3=new TextField();
[Link](50,150,150,20);
[Link](false);
b1=new Button("+");
[Link](50,200,50,50);
b2=new Button("-");
[Link](120,200,50,50);
[Link](this);
[Link](this);
add(tf1);add(tf2);add(tf3);add(b1);add(b2);
setSize(300,300);
setLayout(null);
setVisible(true);
public void actionPerformed(ActionEvent e)
{
String s1=[Link]();
String s2=[Link]();
int a=[Link](s1);
int b=[Link](s2);
int c=0;
if([Link]()==b1){
c=a+b;
}else if([Link]()==b2){
c=a-b;
}
String result=[Link](c);
[Link](result);
}
public static void main(String[] args)
{
TextFieldExample txtfd= new TextFieldExample();
}
}