INTRODUCTION TO ABSTRACT WINDOWING TOOLKIT(AWT)
12 Marks
UNIT 1- AWT(Abstract windowing toolkit)
• Java AWT or Abstract Window Toolkit is an API used for
developing GUI(Graphic User Interfaces) or Window-Based
Applications in Java
• Java AWT is part of the Java Foundation Classes (JFC) that
provides a way to build platform-independent graphical
applications.
• The java.awt package contains AWT API classes such
as TextField, Label, TextArea, RadioButton, CheckBox, Choice,
List etc.
• AWT is heavyweight, which means that its components
consume resources from the underlying operating system (OS)
AWT is Platform Independent?
• The Java AWT utilizes the native platform subroutine to create API
components such as TextField, CheckBox, and buttons.
• This results in a different visual format for these components on different
platforms such as Windows, MAC OS, and Unix.
• The reason for this is that each platform has a distinct view of its native
components
• Java AWT Hierarchy
Container
➢ Container is a component in AWT that contains
another component like button, text field, tables etc.
➢ Container is a subclass of component class. Container
class keeps track of components that are added to
another component.
➢ A container is responsible for laying out (that is
positioning) any components that it contains.
➢ It does this through the use of various layout managers.
Panel
➢ Panel class is a concrete subclass of Container.
➢ Panel is the super-class for Applet. When screen
output is directed to an applet, it is drawn on the
surface of a Panel object.
➢ Panel does not contain title bar, menu bar or
border. It is container that is used for holding
components.
➢ To create panel,
Panel p=new panel();
Window
➢ The Window class creates a top-level window.
➢ A top -level window is not contained withinany
other object; it sits directly on the desktop.
➢ Generally, we won’t create Window objects
directly. Instead, we will use a subclass of
Window called Frame.
Canvas
➢ Canvas is not there in the hierarchy but still it exists
➢ Canvas encapsulates a blank window upon which we can draw.
Frame• Frame is a subclass of Window and have resizing canvas. It is a container that
contain several different components like button, title bar, textfield, label etc. In
Java, most of the AWT applications are created using Frame window.
• Frame class has two different constructors
• Frame() throws HeadlessException
• Frame(String title) throws HeadlessException
Creating a Frame
There are two ways to create a Frame. They are,
• By Instantiating Frame class
• By extending Frame class
AWT Classes
AWT Classes
AWT Classes
AWT Classes
AWT Classes
➢Setting a Window’s Title
void setTitle(String newTitle)
➢ Closing a Frame Window
setVisible(false).
• you must implement the windowClosing( ) method of
the WindowListener interface
• Inside windowClosing( ), you must remove the window
from the screen
AWT CONTROLS
The controls are different components which allows a
user to interact with our application in various wayss.
For adding contols in a window,first create an instance
of the required control and then add it to a window by
calling add() method.
Syntax
Component add(Component compObj)
Here, compObj is an instance of the control that we
want to add.
For removing controls from a window,call
remove() method
Syntax
void remove(Component obj)
Here, obj is a reference to the control that we want
to remove. We can remove all controls by calling
removeAll( ).
TYPES OF CONTROLS
Passive Control
This are the control which do not generate any
event Ex Label
Active Control
This are the control which generate certain
event Ex Button,List,Scrollbar etc
AWT CONTROLS
Label
Button
Checkbox
Checkbox Group
Choice
List
Scrollbar
TextField
TextArea
LABEL
Labels are passive controls that do not support any
interaction with the user.
Constructors
Label( )
Label(String str)
Label(String str, int how)
Where as
str- String to display which is left aligned. how-
how the alignment. I
t’s value can be set as,
Label.LEFT, Label.RIGHT,
Label.CENTER
Methods
1) void setText(String str)
2) String getText()
3) void setAlignment(int how) : Sets the alignment
of the string within the label.
4)int getAlignment() : Allow you to set the
alignment and also to get the the current
alignment.
PROGRAM TO DEMONSTRATE USE OF LABEL
import java.awt.*;
public class LabelEx
{
public static void main(String[] args)
{
Label one=new Label("ONE", Label.LEFT);
Label two=new Label("TWO");
Frame f=new Frame("Label Frame");
f.add(one);
f.add(two);
f.setVisible(true);
f.setSize(500,300);
one.setBounds(80, 80, 50, 20);
two.setBounds(80,80,30,20);
}
}
Points to Remember:
• While creating a frame (either by instantiating or
extending Frame class),
• Following two attributes are must for visibility of the
frame:
• setSize(int width, int height);
• setVisible(true);
• When you create other components like Buttons,
TextFields, etc.
• Then you need to add it to the frame by using the method
- add(Component's Object);
• You can add the following method also for resizing the
frame - setResizable(true);
setBounds() method
• The setBounds() method is used to set the x and y coordinates, as well as the
width and height of a component. Its signature is as follows:
• void setBounds(int x, int y, int width, int height)
• x: The x-coordinate of the component's top-left corner.
• y: The y-coordinate of the component's top-left corner.
• width: The width of the component.
• height: The height of the component.
• AWT Button
• In Java, AWT contains a Button Class. It is used for
creating a labeled button which can perform an
action.
BUTTON
It is a component which has a label andit
generates an event when it is pressed.
Constructors:
Button( )
Button(String str)
Methods:
void setLabel(String str)
String getLabel()
Creating Frame window by extending Frame class
• import java.awt.Button;
• import java.awt.Frame;
•
• public class ButtonEx extends Frame
• {
• ButtonEx()
• {
• Button y=new Button("YES");
• Button n=new Button("NO");
• Button MN=new Button("MAY not decided");
• add(y);
• add(n);
• add(MN);
• setSize(200,200);
• y.setBounds(50,80,40,30);
• MN.setBounds(50,100,40,30);
• n.setBounds(50,120,40,30);
• setVisible(true);
• }
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.
TextField is a subclass of TextComponent.
Constructors:
1) TextField() : Default text field.
2) TextField(int numChars) : specifies the number
of characters it can hold.
3) TextField(String str) : It initializes the textfield
with the string specified.
4) TextField(String str, int numChars) : initializes a
textfield and sets it width.
▪ Events:
TextListener textChanged() TextEvent
Methods:
1) String getText() : to obtain the string currently in the textfield.
2) void setText(String str) : to set the text.
3) Void select(int startindex,int endindex) : you can select portion of text under program
control.
4) String getSelectedText() : to obtain the currently selected text.
5)Void setEditable(boolean edit) : allows you to control a textfield may be modified by
the user. void setEditable(Boolean canEdit)
6)isEditable() : to deteremine whether it is editable or not.
Boolean isEditable()
7) setEchoChar() : to set echoing of character as they are typed.
void setEchoChar(char ch)
8) Char getEchoChar() : to retrieve the echochar
AWT TextField
In Java, AWT contains aTextField Class. It is used for displaying single line text.
• import java.awt.*;
• class TextFieldDemo1{
• public static void main(String args[])
• {
• Frame TextF_f= new Frame("studytonight ==>TextField");
• TextField text1,text2;
• text1=new TextField("Welcome to studytonight");
• text1.setBounds(60,100, 230,40);
• text2=new TextField("This tutorial is of Java");
• text2.setBounds(60,150, 230,40);
• TextF_f.add(text1);
• TextF_f.add(text2);
• TextF_f.setSize(500,500);
• TextF_f.setLayout(null);
• TextF_f.setVisible(true);
• }
• }
•
• AWT TextArea
• In Java, AWT contains aTextArea Class. It is used for displaying
multiple-line text.
TEXT AREA
It is a multi line editor byAWT.
Constructors:
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)