Abstract Windowing Toolkit AWT
Abstract Windowing Toolkit AWT
Abstract Windowing Toolkit AWT
AWT
Introduction
• Java AWT (Abstract Windowing Toolkit) is an API to develop
GUI or window-based application in java.
• Java AWT components are platform-dependent i.e.
components are displayed according to the view of operating
system.
• AWT is heavyweight i.e. its components uses the resources of
system.
• The java.awt package provides classes for AWT API such as
TextField, Label, TextArea, RadioButton, CheckBox, Choice,
List etc.
GUI
AWT Hierarchy
Object
• The Object class is the top most class and parent of all
the classes in java by default.
• Every class in java is directly or indirectly derived from
the object class.
Component
• The Component is abstract class that encapsulates all
the attributes of visual component.
• All User interface (UI) elements that are displayed on
screen are subclasses of Component.
Method Description
• Methods
• void setText(String str)
• String getText( )
• void setAlignment(int how)
• int getAlignment( )
Controls
// Demonstrate Labels
public void init()
import java.awt.*; {
import java.applet.*; Label one = new Label("One");
/* Label two = new Label("Two");
Label three = new Label("Three");
<applet code="LabelDemo" // add labels to applet window
width=300 height=200>
add(one);
</applet> add(two);
*/ add(three);
public class LabelDemo }
extends Applet }
{
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 are objects of type Button.
• Button defines these two constructors:
• Button( )
• Button(String str)
Buttons
• String getLabel()
• void setLabel(String str)
• void setEnabled(Boolean enable)
• Void addActionListener(ActionListener l)
• void removeActionListener(ActionListener l)
• String getActionCommand()
• void setActionCommand(String Cmd)
Button yes, no, maybe;
// Demonstrate Buttons public void init()
import java.awt.*; {
import java.applet.*; yes = new Button("Yes");
/* no = new Button("No");
<applet code="ButtonDemo" maybe = new Button(“Understand");
width=250 height=150> add(yes);
</applet> add(no);
*/ add(maybe);
public class ButtonDemo extends }
Applet public void paint(Graphics g)
{ {
String msg = ""; g.drawString(msg, 6, 100);
}
}
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.
• We change the state of a check box by clicking on it.
Check boxes can be used individually or as part of a
group.
Checkbox constructors:
• Checkbox( )
• Checkbox(String str)
• Checkbox(String str, boolean on)
• Checkbox(String str, boolean on, CheckboxGroup cbGroup)
• Checkbox(String str, CheckboxGroup cbGroup, boolean on)
Methods
• boolean getState( )
• void setState(boolean on)
• String getLabel( )
• void setLabel(String str)
• void addItemListener(ItemListener l)
• void removeItemListener(ItemListener l)
public void init()
// Demonstrate check boxes. {
import java.awt.*; Win98 = new Checkbox("Windows 98/XP",
import java.applet.*; null, true);
winNT = new Checkbox("Windows
/*
NT/2000");
<applet code="CheckboxDemo" solaris = new Checkbox("Solaris");
width=250 height=200> mac = new Checkbox("MacOS");
</applet> add(Win98);
*/ add(winNT);
public class CheckboxDemo extends add(solaris);
add(mac);
Applet
}
{ public void paint(Graphics g)
String msg = ""; {}
Checkbox Win98, winNT, solaris, mac; }
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 button.
• Check box groups are objects of type
CheckboxGroup.
• Only the default constructor is defined, which creates
an empty group.
Methods
Checkbox getSelectedCheckbox( )
void setSelectedCheckbox(Checkbox wh)
public void init()
{
import java.awt.*;
import java.applet.*;
cbg = new CheckboxGroup();
/* Win98 = new Checkbox("Windows 98/XP", cbg,
<applet code="CBGroup" true);
width=250 height=200> winNT = new Checkbox("Windows NT/2000",
</applet> cbg, false);
*/
public class CBGroup extends Applet
solaris = new Checkbox("Solaris", cbg,
{ false);
String msg = ""; mac = new Checkbox("MacOS", cbg, false);
Checkbox Win98, winNT, add(Win98); add(winNT);
solaris, mac; add(solaris); add(mac);}
CheckboxGroup cbg; public void paint(Graphics g)
{
msg = "Current selection: ";
msg +=
cbg.getSelectedCheckbox().getLabel();
g.drawString(msg, 6, 100);
}}
Choice Controls
• 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.
• 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.
Methods
void add(String name)
String getSelectedItem( )
int getSelectedIndex( )
int getItemCount( )
void select(int index)
void select(String name)
String getItem(int index)
import java.awt.*;
import java.applet.*; os.add("Solaris");
/* os.add("MacOS");
<applet code="ChoiceDemo" browser.add("Netscape 3.x");
width=300 height=180> browser.add("Netscape 4.x");
</applet> browser.add("Netscape 5.x");
*/ browser.add("Netscape 6.x");
public class ChoiceDemo extends browser.add("Internet Explorer 4.0");
Applet browser.add("Internet Explorer 5.0");
{ browser.add("Internet Explorer 6.0");
Choice os, browser; browser.add("Lynx 2.4");
String msg = ""; browser.select("Netscape 4.x");
public void init() add(os);
{ add(browser);
os = new Choice(); }
browser = new Choice(); public void paint(Graphics g)
os.add("Windows 98/XP"); {}}
os.add("Windows NT/2000");
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.
List
• 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 (others can be scrolled into
view as needed).
• In the third form, if multipleSelect is true, then the user may select two
or more items at a time.
Methods
void add(String name)
void add(String name, int index)
String getSelectedItem( )
int getSelectedIndex( )
String[ ] getSelectedItems( )
int[ ] getSelectedIndexes( )
int getItemCount( )
void select(int index)
String getItem(int index)
ScrollBars
• 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.
• slider box (or thumb) for the scroll bar.
• The slider box can be dragged by the user to a new
position, this action translates into some form of page
up and page down.
Constructors
• Scrollbar( )
• Scrollbar(int style)
• Scrollbar(int style, int iValue, int tSize, int min, int
max)
• The first form creates a vertical scroll bar.
• The second and third forms allow us to specify style
Scrollbar.VERTICAL, Scrollbar.HORIZONTAL.
• In the third form, the initial value of the scroll bar is passed
in iValue. The number of units represented by the height of
the thumb is passed in tSize. The minimum and maximum
values for the scroll bar are specified by min and max.
Methods
• TextField( )
• TextField(int numChars)
• TextField(String str)
• TextField(String str, int numChars)
TextField Methods
• String getText( )
• void setText(String str)
• String getSelectedText( )
• void select(int startIndex, int endIndex)
• boolean isEditable( )
• void setEditable(boolean canEdit)
• void setEchoChar(char ch)
• boolean echoCharIsSet( )
• char getEchoChar( )
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.
• 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)