AWT Classes
AWT Classes
AWT (Abstract Window Toolkit) was Java’s first GUI framework, which was
introduced in Java 1.0. It is used to create GUIs (Graphical User Interfaces).
AWT Classes
The AWT framework contains many classes and interfaces using which we can
create GUIs. The AWT package is java.awt. Some of the frequently used AWT
classes are listed below:
INTRODUCTION TO AWT
Component
Component is the abstract class that encapsulates all the properties of a visual
component. Except for menus, most of the GUI components are inherited from the
Component class.
Container
Container is a sub class of the Component class which can be used to hold other
components. A Container object can hold other Containers also. A Container is
responsible for laying out (positioning) the components.
Panel
Panel class is a concrete sub class of the Container class. A Panel object is a
window without title bar, menu bar and border. Panel is the super class of Applet
class and is capable of holding other components or containers.
Window
Window is a sub class of Container class. A Window creates a top-level container
which can hold other components or containers.
Frame
Frame is a concrete sub class of Window class. The Frame encapsulates a window.
Frame contains a title bar, menu bar, borders and resizable corners. To create stand
alone applications in Java, we generally use Frame.
Frame Class
The Frame class is used to create standard windows. Following are two Frame
class constructors:
Frame()
Frame(String title)
INTRODUCTION TO AWT
Graphics Class
In GUI applications, we can use Graphics class of java.awt package to create
various graphics like lines, rectangles, circles, polygons etc
Let’s look at some of the methods available in the Graphics class:
void drawLine(int startX, startY, endX, endY) – Used to draw a line between twi
points.
void drawRect(int startX, int startY, int width, int height) – Used to draw a
rectangle starting from the top left corner with the given width and height. The
coordinates of the top left corner of the rectangle are startX and startY.
void fillRect(int startX, int startY, int width, int height) – Used to draw a solid
(colored) rectangle with the given parameters.
INTRODUCTION TO AWT
void drawRoundRect(int startX, int startY, int width, int height, int xDiam, int
yDiam) – Used to draw a rounded rectangle whose x-diameter and y-diameter of
the corners is given by xDiam and yDiam respectively.
void fillRoundRect(int startX, int startY, int width, int height, int xDiam, int
yDiam) – Used to draw a solid (colored) rounded rectangle whose x-diameter and
y-diameter of the corners is given by xDiam and yDiam respectively.
void drawOval(int startX, int startY, int width, int height) – Used to draw an
ellipse inside an imaginary rectangle whose dimensions are specified by the given
parameters. We can get a circle by giving the same value for both width and
height.
void fillOval(int startX, int startY, int width, int height) – Used to draw a solid
(colored) ellipse inside an imaginary rectangle whose dimensions are specified by
the given parameters. We can get a circle by giving the same value for both width
and height.
void drawArc(int startX, int startY, int width, int height, int startAngle, int
sweepAngle) – Used to draw an arc inside an imaginary rectangle. The start angle
and the sweep angle are specified by using startAngle and sweepAngle
respectively.
void fillArc(int startX, int startY, int width, int height, int startAngle, int
sweepAngle) – Used to draw a solid (colored) arc inside an imaginary rectangle.
The start angle and the sweep angle are specified by using startAngle and
sweepAngle respectively.
void drawPolygon(int x[], int y[], int numPoints) – Used to draw a polygon whose
x coordinates and y coordinates of the points are specified using the x array and y
array respectively. Number of points are specified using numPoints.
void fillPolygon(int x[], int y[], int numPoints) – Used to draw a solid (colored)
polygon whose x coordinates and y coordinates of the points are specified using
the x array and y array respectively. Number of points are specified using
numPoints.
import java.awt.*;
import java.awt.event.*;
Java
Copy
AWT Controls
We can add and remove controls to a Container like Applet and Frame using the
following methods available in the Container class:
Component add(Component ref)
Component remove(Component ref)
Label
A label is a GUI control which can be used to display static text. Label can be
created using the Label class and its constructors which are listed below:
Label()
Label(String str)
INTRODUCTION TO AWT
The parameter how specifies the text alignment. Valid values are
Label.LEFT,
Label.CENTER or Label.RIGHT
Some of the methods available in the Label class are as follows:
void setText(String str) – To set or assign text to the label.
String getText() – To retrieve the text of a label.
void setAlignment(int how) – To set the alignment of text in a label.
int getAlignment() – To get the alignment of text in a label.
import java.awt.*;
import java.awt.event.*;
Java
Copy
Buttons
A push button is the frequently found GUI control. A push button or a button can
be created by using the Button class and its constructors which are given below:
INTRODUCTION TO AWT
Button()
Button(String str)
import java.awt.*;
import java.awt.event.*;
add(b1);
add(b2);
add(b3);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
}
);
}
public static void main(String[] args)
{
MyFrame mf = new MyFrame();
}
}
Java
Copy
Checkboxes
A checkbox control can be created using the Checkbox class and its following
constructors:
Checkbox()
Checkbox(String str)
Checkbox(String str, boolean on)
Checkbox(String str, boolean on, CheckboxGroup cbGroup)
Checkbox(String str, CheckboxGroup cbGroup, boolean on)
import java.awt.*;
INTRODUCTION TO AWT
import java.awt.event.*;
Java
Copy
INTRODUCTION TO AWT
In AWT, there is no separate class for creating radio buttons. The difference
between a checkbox and radio button is, a user can select one or more checkboxes.
Whereas, a user can select only one radio button in a group.
Radio buttons can be create by using Checkbox class and CheckboxGroup class as
shown in the below code:
import java.awt.*;
import java.awt.event.*;
Java
Copy
Dropdown Boxes
A drop down box or a combo box contains a list of items (strings). When a user
clicks on a drop down box, it pops up a list of items from which user can select a
single item.
A drop down box can be created using the Choice class. There is only one
constructor in the choice class using which we can create an empty list.
Following are various methods available in Choice class:
void add(String name) – To add an item to the drop down list.
String getSelectedItem() – To retrieve the item selected by the user.
int getSelectedIndex() – To retrieve the index of the item selected by the user.
int getItemCount() – To retrieve the number of items in the drop down list.
void select(int index) – To select an item based on the given index.
void select(String name) – To select an item based on the given item name.
void getItem(int index) – To retrieve an item at the given index.
Whenever an user selects an item from the drop down box, an ItemEvent is
generated. It can be handled using the ItemListener interface and the event
handling method is itemStateChanged().
INTRODUCTION TO AWT
import java.awt.*;
import java.awt.event.*;
{
MyFrame mf = new MyFrame();
}
}
Java
Copy
List Boxes
List box contains a list of items among which the user can select one or more
items. More than one items in the list box are visible to the user. A list box can be
created using the List class along with the following constructors:
List()
List(int numRows)
List(int numRows, boolean multipleSelect)
INTRODUCTION TO AWT
import java.awt.*;
import java.awt.event.*;
INTRODUCTION TO AWT
Java
Copy
INTRODUCTION TO AWT
Text Fields
A text field or text box is a single line text entry control which allows the user to
enter a single line of text. a text field can be created using the TextField class along
with its following constructors:
TextField()
TextField(int numChars)
TextField(String str)
TextField(String str, int numChars)
In the above constructors numChars specifies the width of the text field, and str
specifies the initial text in the text field.
When an user hits ‘Enter’ key on the keyboard in a text field, an ActionEvent is
generated. It can be handled using ActionListener and the event handling method is
actionPerformed().
INTRODUCTION TO AWT
Whenever an user modifies the text in the text field, a TextEvent is generated
which can be handled using TextListener and the event handling method is
textValueChanged().
import java.awt.*;
import java.awt.event.*;
setSize(400, 200);
setTitle("My Application");
setLayout(new FlowLayout());
myLabel = new Label("Enter name: ");
tf = new TextField(20);
add(myLabel);
add(tf);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
}
);
setVisible(true);
}
public static void main(String[] args)
{
MyFrame mf = new MyFrame();
}
}
Java
Copy
Text Areas
A text area is a multi-line text entry control in which user can enter multiple lines
of text. A text area can be created using the TextArea class along with the
following 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)
In the above constructors, numLines specifies the height of the text area, numChars
specifies the width of the text area, str specifies the initial text in the text area and
sBars specifies the scroll bars. Valid values of sBars can be any one of the
following:
SCROLLBARS_BOTH
SCROLLBARS_NONE
INTRODUCTION TO AWT
SCROLLBARS_HORIZONTAL_ONLY
SCROLLBARS_VERTICAL_ONLY
import java.awt.*;
import java.awt.event.*;
setLayout(new FlowLayout());
ta = new TextArea(3, 20);
add(ta);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
}
);
setVisible(true);
}
public static void main(String[] args)
{
MyFrame mf = new MyFrame();
}
}
Java
Copy
Layout Managers:
A layout manager is one which automatically manages the arrangement of various
of components in a container.
Panel – Flow Layout
JPanel – Flow Layout
Applet – Flow Layout
JApplet – Border Layout
Frame – Border Layout
JFrame – Border Layout
1. FlowLayout
2. BorderLayout
3. GridLayout
4. CardLayout
INTRODUCTION TO AWT
5. GridBagLayout
FlowLayout Manager
The flow layout manager arranges the components one after another from left-to-
right and top-to-bottom manner.
layout manager instance can be created using anyone of the following constructors:
FlowLayout()
FlowLayout(int how)
FlowLayout(int how, int hspace, int vspace)
In the above constructors, how specifies the alignment, hspace specifies horizontal
space, and vspace specifies vertical space. Valid values for alignment are as
follows:
FlowLayout.LEFT
FlowLayout.CENTER
FlowLayout.RIGHT
FlowLayout.LEADING
FlowLayout.TRAILING
Following code demonstrates working with FlowLayout:
import java.awt.*;
import java.awt.event.*;
System.exit(0);
}
}
);
setVisible(true);
}
public static void main(String[] args)
{
MyFrame mf = new MyFrame();
}
}
Java
Copy
BorderLayout Manager
The border layout manager divides the container area into five regions namely:
north, south, east, west, and center. Default region is center. You have to be careful
with border layout as controls might be stacked over one another. Border layout
instance can be created by using one of the below constructors:
BorderLayout()
BorderLayout(int hspace, int vspace)
import java.awt.*;
import java.awt.event.*;
add(bsouth, BorderLayout.SOUTH);
add(beast, BorderLayout.EAST);
add(bwest, BorderLayout.WEST);
add(bcenter, BorderLayout.CENTER);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
}
);
setVisible(true);
}
public static void main(String[] args)
{
MyFrame mf = new MyFrame();
}
}
Java
Copy
GridLayout Manager
The grid layout manager arranges the components in a 2-dimensional grid. While
creating the instance of GridLayout, we can specify the number of rows and
columns in the grid. Care must be taken with the number of cells in the grid and
the number of components being added to the grid. If they don’t match, we might
get unexpected output.
An instance of GridLayout can be created using one of the following constructors:
GridLayout()
GridLayout(int numRows, int numCols)
GridLayout(int numRows, int numCols, int hspace, int vspace)
In the above constructors, numRows and numCols specifies the number of rows
and columns in the grid, hspace and vsapce specifies the horizontal space and
vertical space between the components.
import java.awt.*;
import java.awt.event.*;
INTRODUCTION TO AWT
Java
Copy
CardLayout Manager
The card layout manager allows the user to create a deck of cards. Each card can
contain different components. At any instant only one card in the deck can be
displayed.
To implement card layout, we must take a panel which acts as the container for
other cards. Each card in turn can be a panel which can contain different
INTRODUCTION TO AWT
components. Components will be added to the respective cards (panels) and all the
cards will be finally added to the deck (container panel).
The card layout can be instantiated using any one of the following constructors:
CardLayout()
CardLayout(int hspace, int vspace)
When adding the cards (panels) to the deck, the following add() method can be
used:
In the above syntax, name is a string which represents the name of the card
(panel). After adding all the cards (panels) to the deck, we can navigate through the
cards using the following methods available in CardLayout class:
import java.awt.*;
import java.awt.event.*;
INTRODUCTION TO AWT
Java
Copy