awt
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( )
import java.awt.*; import java.awt.event.*;
public class MyFrame extends Frame
{
Label myLabel; MyFrame()
{
setSize(400, 200);
setTitle("My Application");
setLayout(new FlowLayout());
setVisible(true);
myLabel = new Label("This is a label!");
add(myLabel);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent we){ System.exit(0);
}
}
}
public static void main(String[] args){ MyFrame mf = new MyFrame();
}
}
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)
import java.awt.*; windowClosing(WindowEvent we)
import java.awt.event.*; {
public class MyFrame extends System.exit(0);
Frame
}
{
Button r,g,b; }
MyFrame() }
{ public static void main(String[]
setSize(400, 200); args)
setTitle("My Application"); {
setLayout(new FlowLayout()); MyFrame mf = new MyFrame();
setVisible(true);
}
r = new Button("Red");
g = new Button("Green"); }
b = new
Button("Blue"); add(r); add(g);
add(b);
addWindowListener(new
WindowAdapter()
{
public void
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)
import java.awt.*; addWindowListener(new
Import java.awt.event.*; WindowAdapter()
public class MyFrame extends {
Frame public void
{ windowClosing(WindowEvent we)
Checkbox c1,c2; {
MyFrame() System.exit(0);
{ }
setSize(400, 200); }
setTitle("My }
Application"); public static void main(String[]
setLayout(new FlowLayout()); args){
setVisible(true); MyFrame mf = new MyFrame();
c1 = new Checkbox("Male"); }
c2 = new Checkbox("Female"); }
add(c1);
add(c2);
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)
import java.awt.*; import
java.awt.event.*;
• 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)