You are on page 1of 22

JAVA AWT

Java AWT
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.
Java AWT Hierarchy
The hierarchy of Java AWT classes are given below.
Component Class
Component class is at the top of AWT hierarchy.
Component class is an abstract class that encapsulates all of the
attributes of a visual component.
All user interface elements that are displayed on the screen and that
interact with the users are subclasses of component. If defines a
number of methods that are responsible for managing events.
Important methods of Component class
Following are the important methods, practiced very often, of Component class that can
be used by all the sub classes (components).
setEnabled(boolean): The parameter false makes the component disabled so that it does not
respond to user interactions (say, clicks).
setBounds(): Using this method, the programmer can give his own size to the component in terms
of width and height and also the location where he can place the component in the container. This
method is not used often as the programmer prefers to place the component in the container using
layout managers.
getWidth(): The programmer can obtain the width of the component.
getHeight(): The programmer can obtain the height of the component.
paint(): Used very extensively to draw graphics. This method is called implicitly when the frame is
created or resized.
repaint(): Used by the programmer to call the paint() method explicitly anywhere in the program.
setBackground(): Used to give a background color to a component.
setForeground(): Used to give a foreground color to a component.
setFont(): Used to give a font to a component to display the text.
getSize(): Returns the size of the component.
setSize(): Used to give the size to the component.
update(): The is method is called implicitly by the repaint(). A call to this method clears the earlier
drawings existing on the container.
Container class
The container class is a subclass of component class. It has additional
methods that allow other component objects to be nested in it.
A container is responsible for laying out any components that it
contains. It does this through the use of various layout managers.
Java AWT Hierarchy
The hierarchy of Java AWT classes are given below.
Important methods in Container class
Following are the important methods of the Container class.
add(): This method is overloaded with which a component can be
added to the container.
invalidate(): Used to invalidate the present set up of components
in the container.
validate(): Used to revalidate the current set up of components
after calling invalidate().
The important containers used very often are applets, frames. Rare are
dialog boxes.
Layout Managers
Each container has a layout manager associated with it.
The LayoutManagers are used to arrange components in a particular
manner. LayoutManager is an interface that is implemented by all the
classes of layout managers. There are following classes that represents
the layout managers:
java.awt.BorderLayout
java.awt.FlowLayout
java.awt.GridLayout
java.awt.CardLayout
java.awt.GridBagLayout
The layout manager is set by the setLayout() method. If no call is
made to setLayout(), then default layout manager is used.
Whenever a container is resized, the layout manager is used to position
each of the components within it.
FlowLayout(Default Layout)
The FlowLayout is used to arrange the components in a line, one after another (in a flow). It is the
default layout manager.

Constructors of FlowLayout class

FlowLayout(): creates a flow layout with centered alignment and a default 5 pixels horizontal
and vertical gap.
FlowLayout(int align): creates a flow layout with the given alignment and a default 5 pixels
horizontal and vertical gap.
FlowLayout(int align, int hgap, int vgap): creates a flow layout with the given alignment and
the given horizontal and vertical gap.

Fields of FlowLayout class


public static final int LEFT
public static final int RIGHT
public static final int CENTER
FlowLayout Example
import java.awt.*;
public class FlowLayoutDemo{
Frame f;
FlowLayoutDemo(){
f=new Frame();
Button b1=new Button("1");
Button b2=new Button("2");
Button b3=new Button("3");
f.add(b1);f.add(b2);f.add(b3);
f.setLayout(new FlowLayout(FlowLayout.LEFT)); //setting flow layout of left alignment
f.setSize(300,300);
f.setVisible(true);
}
public static void main(String[] args) {
new FlowLayoutDemo();
}
}
BorderLayout
The BorderLayout is used to arrange the components in five regions: north, south, east, west and
center. Each region (area) may contain one component only. The BorderLayout provides five
constants for each region:
public static final int NORTH
public static final int SOUTH
public static final int EAST
public static final int WEST
public static final int CENTER

Constructors of BorderLayout class:


BorderLayout(): creates a border layout but with no gaps between the components.
BorderLayout(int hgap, int vgap): creates a border layout with the given horizontal and
vertical gaps between the components.
Border Layout Example
import java.awt.*; f.add(b4,BorderLayout.WEST);
public class BorderLayoutDemo { f.add(b5,BorderLayout.CENTER);
Frame f; f.setSize(300,300);
BorderLayoutDemo(){ f.setVisible(true);
f=new Frame(); }
Button b1=new Button("NORTH"); public static void main(String[] args) {
Button b2=new Button("SOUTH"); new BorderLayoutDemo();
Button b3=new Button("EAST"); }
Button b4=new Button("WEST"); }
Button b5=new Button("CENTER");
f.add(b1,BorderLayout.NORTH);
f.add(b2,BorderLayout.SOUTH);
f.add(b3,BorderLayout.EAST);
Grid Layout
GridLayout lays out components in a two-dimensional grid. When you instantiate
a GridLayout, you define the number of rows and columns.
The GridLayout is used to arrange the components in rectangular grid. One
component is displayed in each rectangle.
Constructors of GridLayout class
GridLayout(): creates a grid layout with one column per component in a row.
GridLayout(int rows, int columns): creates a grid layout with the given rows
and columns but no gaps between the components.
GridLayout(int rows, int columns, int hgap, int vgap): creates a grid layout
with the given rows and columns alongwith given horizontal and vertical gaps.
Grid Layout Example
import java.awt.*; f.add(b1);f.add(b2);f.add(b3);f.add(b4);f.add(b5);
public class GridLayoutDemo{ f.add(b6);f.add(b7);
Frame f; f.setLayout(new GridLayout(3,3));
GridLayoutDemo(){ f.setSize(300,300);
f=new Frame(); f.setVisible(true); }
Button b1=new Button("1"); public static void main(String[] args) {
Button b2=new Button("2"); new GridLayoutDemo();
Button b3=new Button("3"); }
Button b4=new Button("4"); }
Button b5=new Button("5");
Button b6=new Button("6");
Button b7=new Button("7");
Card layout
The CardLayout class manages the components in such a manner that only one
component is visible at a time. It treats each component as a card that is why it is
known as CardLayout.
Constructors of CardLayout class
CardLayout(): creates a card layout with zero horizontal and vertical gap.
CardLayout(int hgap, int vgap): creates a card layout with the given horizontal and vertical
gap.
Commonly used methods of CardLayout class
public void next(Container parent): is used to flip to the next card of the given container.
public void previous(Container parent): is used to flip to the previous card of the given
container.
public void first(Container parent): is used to flip to the first card of the given container.
public void last(Container parent): is used to flip to the last card of the given container.
public void show(Container parent, String name): is used to flip to the specified card with
the given name.
Card Layout Example
import java.awt.*; b3.addActionListener(this);
import java.awt.event.*; c.add("a",b1);c.add("b",b2);c.add("c",b3); }
import javax.swing.*; public void actionPerformed(ActionEvent e) {
public class CardLayoutDemo extends JFrame implements card.next(c); }
ActionListener{
public static void main(String[] args) {
CardLayout card;
CardLayoutDemo cl=new CardLayoutDemo();
Button b1,b2,b3;
cl.setSize(400,400);
Container c;
cl.setVisible(true);
CardLayoutDemo(){
cl.setDefaultCloseOperation(EXIT_ON_CLOSE);
c=getContentPane();
}
card=new CardLayout(50,50);
}
c.setLayout(card);
b1=new Button("Apple");
b2=new Button("Boy");
b3=new Button("Cat");
b1.addActionListener(this);
b2.addActionListener(this);
Insets Class
If you want to leave a small space between the container that holds
your components and the window that contains it. To do this, override
the getInsets() method that is defined by the container.

Insets(int top, int left, int bottom, int right)

The values passed in top, left, bottom and right specify the amount of
space between the container and its enclosing window.
Insets Example
import java.awt.*;
import java.applet.*;

public class InsetsDemo extends Applet


{
public void init()
{
setBackground(Color.cyan);
setLayout(new BorderLayout());
add(new Button("RMI"),BorderLayout.NORTH);
add(new Button("SERVLET"),BorderLayout.EAST);
add(new Button("JDBC"),BorderLayout.SOUTH);
add(new Button("BEANS"),BorderLayout.WEST);
add(new Button("JAVA"),BorderLayout.CENTER);
}

public Insets getInsets()


{
return new Insets(10,20,10,20);
}
}
Dimension Class
The Dimension class encapsulates the width and height of a
component (in integer precision) in a single object.
This class is associated with certain properties of components. Several
methods defined by the Component class and the LayoutManager
interface return a Dimension object.
Normally the values of width and height are non-negative integers.
The constructors that allow you to create a dimension do not prevent
you from setting a negative value for these properties.
If the value of width or height is negative, the behavior of some
methods defined by other objects is undefined.
Setting Window Dimension
The setSize() method is used to set the dimensions of the window.
The signature is:
void setSize(int width, int height)
void setSize()Dimension size)
The getSize() method is used to return or obtain the current size of the
window.
The signature is :
Dimension getSize()
This method returns the current zise of the window within the width
and height fields of a Dimension object

You might also like