You are on page 1of 23

The AWT contains numerous classes and methods that allow you to create and

manage windows. It is also the foundation upon which Swing is built.

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.

Basic Terminologies
Term Description

Component Component is an object having a graphical representation that


can be displayed on the screen and that can interact with the
user. For examples buttons, checkboxes, list and scrollbars of a
graphical user interface.

Container Container object is a component that can contain other


components.Components added to a container are tracked in a
list. The order of the list will define the components' front-to-
back stacking order within the container. If no index is specified
when adding a component to a container, it will be added to the
end of the list.

Panel Panel provides space in which an application can attach any


other components, including other panels.

Window The window is the container that have no borders and menu
bars. Window is a rectangular area which is displayed on the
screen. In different window we can execute different program
and display different data. Window provide us with multitasking
environment. A window must have either a frame, dialog, or
another window defined as its owner when it's constructed.

Frame A Frame is a top-level window with a title and a border. The


size of the frame includes any area designated for the border.
Frame encapsulates window. It and has a title bar, menu bar,
borders, and resizing corners.

Canvas Canvas component represents a blank rectangular area of the


screen onto which the application can draw. Application can
also trap input events from the use from that blank area of
Canvas component.
Java AWT Hierarchy

The hierarchy of Java AWT classes are given below.


Useful Methods of Component class

Method Description

public void add(Component c) inserts a component on


this component.

public void sets the size (width and


setSize(intwidth,int height) height) of the
component.

public void defines the layout


setLayout(LayoutManager m) manager for the
component.

public void setVisible(boolean changes the visibility


status) of the component, by
default false.

To create simple awt example, you need a frame. There are two ways to create a
frame in AWT.

o By extending Frame class (inheritance)


o By creating the object of Frame class (association)

Simple example of AWT by inheritance


1. import java.awt.*;
2. class First extends Frame{
3. First(){
4. Button b=new Button("click me");
5. b.setBounds(30,100,80,30);// setting button position
6. add(b);//adding button into frame
7. setSize(300,300);//frame size 300 width and 300 height
8. setLayout(null);//no layout manager
9. setVisible(true);//now frame will be visible, by default not visible
10.}
11.public static void main(String args[]){
12.First f=new First();
13.}}

The setBounds(intxaxis, intyaxis, int width, int height) method is used in the above
example that sets the position of the awt button.

Example of AWT by association


1. import java.awt.*;
2. class First2{
3. First2(){
4. Frame f=new Frame();
5. Button b=new Button("click me");
6. b.setBounds(30,50,80,30);
7. f.add(b);
8. f.setSize(300,300);
9. f.setLayout(null);
10.f.setVisible(true);
11.}
12.public static void main(String args[]){
13.First2 f=new First2();
14.}}

AWT UI Elements:
Every user interface considers the following three main aspects:

 UI elements :Thes are the core visual elements the user eventually sees and
interacts with. GWT provides a huge list of widely used and common
elements varying from basic to complex

 Layouts: They define how UI elements should be organized on the screen


and provide a final look and feel to the GUI (Graphical User Interface).

Following is the list of commonly used


controls while designed GUI using AWT.
Sr. Control & Description
No.

1 Label

A Label object is a component for placing text in a container.

2 Button

This class creates a labeled button.


3 Check Box

A check box is a graphical component that can be in either


an on (true) or off (false) state.

4 Check Box Group

The CheckboxGroup class is used to group the set of checkbox.

5 List

The List component presents the user with a scrolling list of text
items.

6 Text Field

A TextField object is a text component that allows for the editing of a


single line of text.

7 Text Area

A TextArea object is a text component that allows for the editing of a


multiple lines of text.

8 Choice

A Choice control is used to show pop up menu of choices. Selected


choice is shown on the top of the menu.

9 Canvas

A Canvas control represents a rectangular area where application can


draw something or can receive inputs created by user.

10 Image
An Image control is superclass for all image classes representing
graphical images.

11 Scroll Bar

A Scrollbar control represents a scroll bar component in order to


enable user to select from range of values.

12 Dialog

A Dialog control represents a top-level window with a title and a


border used to take some form of input from the user.

13 File Dialog

A FileDialog control represents a dialog window from which the user


can select a file.

What is a Layout Manager?


A layout manager is an object that controls the size and position (layout) of
components inside a Container object. For example, a window is a container that
contains components such as buttons and labels. The layout manager in effect for
the window determines how the components are sized and positioned inside the
window.
A container can contain another container. For example, a window can contain a
panel, which is itself a container. As you can see in the figure, the layout manager
in effect for the window determines how the two panels are sized and positioned
inside the window, and the layout manager in effect for each panel determines how
components are sized and positioned inside the panels.
Layout Managers
The java.awt package provides the following predefined layout managers that
implement the java.awt.LayoutManager interface. Every Abstract Window Toolkit
(AWT) and Swing container has a predefined layout manager as its default. It is
easy to use the container.setLayout method to change the layout manager, and you
can define your own layout manager by implementing the
java.awt.LayoutManager interface.

 BorderLayout
 BoxLayout
 CardLayout
 FlowLayout
 GridBagLayout
 GridLayout
 GroupLayout
 SpringLayout

BorderLayout

Every content pane is initialized to use a BorderLayout. (As Using Top-Level


Containers explains, the content pane is the main container in all frames, applets,
and dialogs.) A BorderLayout places components in up to five areas: top, bottom,
left, right, and center. All extra space is placed in the center area. Tool bars that are
created using JToolBar must be created within a BorderLayout container, if you
want to be able to drag and drop the bars away from their starting positions..The
BorderLayout provides five constants for each region:

1. public static final int NORTH


2. public static final int SOUTH
3. public static final int EAST
4. public static final int WEST
5. public static final int CENTER

Constructors of BorderLayout class:


o BorderLayout(): creates a border layout but with no gaps between the
components.
o JBorderLayout(inthgap, intvgap): creates a border layout with the given
horizontal and vertical gaps between the components.

Example of BorderLayout class:

1. import java.awt.*;
2. import javax.swing.*;
3.
4. public class Border {
5. JFrame f;
6. Border(){
7. f=new JFrame();
8.
9. JButton b1=new JButton("NORTH");;
10. JButton b2=new JButton("SOUTH");;
11. JButton b3=new JButton("EAST");;
12. JButton b4=new JButton("WEST");;
13. JButton b5=new JButton("CENTER");;
14.
15. f.add(b1,BorderLayout.NORTH);
16. f.add(b2,BorderLayout.SOUTH);
17. f.add(b3,BorderLayout.EAST);
18. f.add(b4,BorderLayout.WEST);
19. f.add(b5,BorderLayout.CENTER);
20.
21. f.setSize(300,300);
22. f.setVisible(true);
23.}
24.public static void main(String[] args) {
25. new Border();
26.}
27.}

BoxLayout

The BoxLayout class puts components in a single row or column. It respects the
components' requested maximum sizes and also lets you align components.For this
purpose, BoxLayout provides four constants. They are as follows:
Note: BoxLayout class is found in javax.swing package.

Fields of BoxLayout class:


1. public static final int X_AXIS
2. public static final int Y_AXIS
3. public static final int LINE_AXIS
4. public static final int PAGE_AXIS

Constructor of BoxLayout class:


1. BoxLayout(Container c, int axis): creates a box layout that arranges the
components with the given axis.

Example of BoxLayout class with Y-AXIS:


1. import java.awt.*;
2. import javax.swing.*;
3.
4. public class BoxLayoutExample1 extends Frame {
5. Button buttons[];
6.
7. public BoxLayoutExample1 () {
8. buttons = new Button [5];
9.
10. for (int i = 0;i<5;i++) {
11. buttons[i] = new Button ("Button " + (i + 1));
12. add (buttons[i]);
13. }
14.
15.setLayout (new BoxLayout (this, BoxLayout.Y_AXIS));
16.setSize(400,400);
17.setVisible(true);
18.}
19.
20.public static void main(String args[]){
21.BoxLayoutExample1 b=new BoxLayoutExample1();
22.}
23.}

Example of BoxLayout class with X-AXIS:

1. import java.awt.*;
2. import javax.swing.*;
3.
4. public class BoxLayoutExample2 extends Frame {
5. Button buttons[];
6.
7. public BoxLayoutExample2() {
8. buttons = new Button [5];
9.
10. for (int i = 0;i<5;i++) {
11. buttons[i] = new Button ("Button " + (i + 1));
12. add (buttons[i]);
13. }
14.
15.setLayout (new BoxLayout(this, BoxLayout.X_AXIS));
16.setSize(400,400);
17.setVisible(true);
18.}
19.
20.public static void main(String args[]){
21.BoxLayoutExample2 b=new BoxLayoutExample2();
22.}
23.}
24.
CardLayout

The CardLayout class lets you implement an area that contains different
components at different times. A CardLayout is often controlled by a combo box,
with the state of the combo box determining which panel (group of components)
the CardLayoutdisplays. An alternative to using CardLayout is using a tabbed
pane, which provides similar functionality but with a pre-defined GUI.

Constructors of CardLayout class:


1. CardLayout(): creates a card layout with zero horizontal and vertical
gap.
2. CardLayout(inthgap, intvgap): creates a card layout with the given
horizontal and vertical gap.

Commonly used methods of CardLayout class:


o public void next(Container parent): is used to flip to the next card of
the given container.
o public void previous(Container parent): is used to flip to the previous
card of the given container.
o public void first(Container parent): is used to flip to the first card of the
given container.
o public void last(Container parent): is used to flip to the last card of the
given container.
o public void show(Container parent, String name): is used to flip to the
specified card with the given name.

Example of CardLayout class:


1. import java.awt.*;
2. import java.awt.event.*;
3. import javax.swing.*;
4.
5. public class CardLayoutExample extends JFrame implements ActionListener{
6. CardLayout card;
7. JButton b1,b2,b3;
8. Container c;
9. CardLayoutExample(){
10.
11. c=getContentPane();
12. card=new CardLayout(40,30);
13.//create CardLayout object with 40 hor space and 30 ver space
14. c.setLayout(card);
15.
16. b1=new JButton("Apple");
17. b2=new JButton("Boy");
18. b3=new JButton("Cat");
19. b1.addActionListener(this);
20. b2.addActionListener(this);
21. b3.addActionListener(this);
22.
23. c.add("a",b1);c.add("b",b2);c.add("c",b3);
24. }
25. public void actionPerformed(ActionEvent e) {
26. card.next(c);
27. }
28.
29. public static void main(String[] args) {
30. CardLayoutExample cl=new CardLayoutExample();
31. cl.setSize(400,400);
32. cl.setVisible(true);
33. cl.setDefaultCloseOperation(EXIT_ON_CLOSE);
34. }
35.}

FlowLayout

FlowLayout is the default layout manager for every JPanel. It simply lays out
components in a single row, starting a new row if its container is not sufficiently
wide. Both panels in CardLayoutDemo, shown previously, use FlowLayout.

Fields of FlowLayout class:


1. public static final int LEFT
2. public static final int RIGHT
3. public static final int CENTER
4. public static final int LEADING
5. public static final int TRAILING
Constructors of FlowLayout class:
1. FlowLayout(): creates a flow layout with centered alignment and a
default 5 unit horizontal and vertical gap.
2. FlowLayout(int align): creates a flow layout with the given alignment
and a default 5 unit horizontal and vertical gap.
3. FlowLayout(int align, inthgap, intvgap): creates a flow layout with the
given alignment and the given horizontal and vertical gap.

Example of FlowLayout class:

1. import java.awt.*;
2. import javax.swing.*;
3.
4. public class MyFlowLayout{
5. JFrame f;
6. MyFlowLayout(){
7. f=new JFrame();
8.
9. JButton b1=new JButton("1");
10. JButton b2=new JButton("2");
11. JButton b3=new JButton("3");
12. JButton b4=new JButton("4");
13. JButton b5=new JButton("5");
14.
15. f.add(b1);f.add(b2);f.add(b3);f.add(b4);f.add(b5);
16.
17. f.setLayout(new FlowLayout(FlowLayout.RIGHT));
18. //setting flow layout of right alignment
19.
20. f.setSize(300,300);
21. f.setVisible(true);
22.}
23.public static void main(String[] args) {
24. new MyFlowLayout();
25.}
26.}

GridLayout

GridLayout simply makes a bunch of components equal in size and displays them
in the requested number of rows and columns..

Constructors of GridLayout class:


1. GridLayout(): creates a grid layout with one column per component in a
row.
2. GridLayout(int rows, int columns): creates a grid layout with the given
rows and columns but no gaps between the components.
3. GridLayout(int rows, int columns, inthgap, intvgap): creates a grid
layout with the given rows and columns alongwith given horizontal and
vertical gaps.

Example of GridLayout class:

1. import java.awt.*;
2. import javax.swing.*;
3.
4. public class MyGridLayout{
5. JFrame f;
6. MyGridLayout(){
7. f=new JFrame();
8.
9. JButton b1=new JButton("1");
10. JButton b2=new JButton("2");
11. JButton b3=new JButton("3");
12. JButton b4=new JButton("4");
13. JButton b5=new JButton("5");
14. JButton b6=new JButton("6");
15. JButton b7=new JButton("7");
16. JButton b8=new JButton("8");
17. JButton b9=new JButton("9");
18.
19. f.add(b1);f.add(b2);f.add(b3);f.add(b4);f.add(b5);
20. f.add(b6);f.add(b7);f.add(b8);f.add(b9);
21.
22. f.setLayout(new GridLayout(3,3));
23. //setting grid layout of 3 rows and 3 columns
24.
25. f.setSize(300,300);
26. f.setVisible(true);
27.}
28.public static void main(String[] args) {
29. new MyGridLayout();
30.}
31.}

GridBagLayout

GridBagLayout is a sophisticated, flexible layout manager. It aligns components


by placing them within a grid of cells, allowing components to span more than one
cell. The rows in the grid can have different heights, and grid columns can have
different widths..
GroupLayout

GroupLayout is a layout manager that was developed for use by GUI builder tools,
but it can also be used manually. GroupLayoutworks with the horizontal and
vertical layouts separately. The layout is defined for each dimension
independently. Consequently, however, each component needs to be defined twice
in the layout. The Find window shown above is an example of a GroupLayout.

SpringLayout

SpringLayout is a flexible layout manager designed for use by GUI builders. It lets
you specify precise relationships between the edges of components under its
control. For example, you might define that the left edge of one component is a
certain distance (which can be dynamically calculated) from the right edge of a
second component.SpringLayout lays out the children of its associated container
according to a set of constraints

You might also like