AWT in java

AWT in java


AWT contains large number of classes and methods that allows you to create and manage graphical user interface ( GUI ) applications, such as windows, buttons, scroll bars,etc. The AWT was designed to provide a common set of tools for GUI design that could work on a variety of platforms. Java AWT components are platform-dependent. The java.awt package provides classes for AWT api such as TextField, Label, TextArea, RadioButton, CheckBox, Choice, List etc.



The Container is a component in AWT that can contain another components like buttons, textfields, labels etc. The classes that extends Container class are known as container such as Frame, Dialog and Panel.


The window is the container that have no borders and menu bars. You must use frame, dialog or another window for creating a window.


The Panel is the container that doesn’t contain title bar and menu bars. It can have other components like button, textfield etc.

  • Frame

The Frame is the container that contain title bar and can have menu bars. It can have other components like button, textfield etc.

Useful Methods of Component class

Method                                                                       Description

public void add(Component c)                       inserts a component on this component.

public void setSize(int width,int height)        sets the size (width and height) of the component.

public void setLayout(LayoutManager m)     defines the layout manager for the component.

public void setVisible(boolean status)            changes the visibility of the component, by default false.

Java AWT Example

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

 1. By extending Frame class (inheritance)

2.   By creating the object of Frame class (association)

AWT Example by Inheritance

Let’s see a simple example of AWT where we are inheriting Frame class. Here, we are showing Button component on the Frame.

import java.awt.*; 

class First extends Frame{ 


Button b=new Button(“click me”); 

b.setBounds(30,100,80,30);    // setting button position 

add(b);                        //adding button into frame 

setSize(300,300);        //frame size 300 width and 300 height 

setLayout(null);           //no layout manager 

setVisible(true);           //now frame will be visible, by default not visible 


public static void main(String args[]){ 

First f=new First(); 


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


AWT Example by Association

Let’s see a simple example of AWT where we are creating instance of Frame class. Here, we are showing Button component on the Frame.

import java.awt.*; 

class First2{ 


Frame f=new Frame(); 

Button b=new Button(“click me”); 







public static void main(String args[]){ 

First2 f=new First2(); 



Event and Listener (Java Event Handling)

Changing the state of an object is known as an event. For example, click on button, dragging mouse etc.

Java Event classes and Listener interfaces

Event Classes                                     Listener Interfaces

ActionEvent                                        ActionListener

MouseEvent                                        MouseListener and MouseMotionListener

MouseWheelEvent                              MouseWheelListener

KeyEvent                                            KeyListener

ItemEvent                                           ItemListener

TextEvent                                           TextListener

AdjustmentEvent                                AdjustmentListener

WindowEvent                                     WindowListener

ComponentEvent                    ComponentListener

ContainerEvent                       ContainerListener

FocusEvent                             FocusListener


Java event handling by implementing ActionListener

import java.awt.*; 

import java.awt.event.*; 

class AEvent extends Frame implements ActionListener{ 

TextField tf; 


  //create components 

tf=new TextField(); 


Button b=new Button(“click me”); 


  //register listener 

b.addActionListener(this);//passing current instance 


//add components and set size, layout and visibility 






public void actionPerformed(ActionEvent e){ 



public static void main(String args[]){ 

new AEvent(); 





AWT is the foundation upon which Swing is made i.e Swing is a set of GUI interfaces that extends the AWT. But now a days AWT is merely used because most GUI Java programs are implemented using Swing because of its rich implementation of GUI controls and light-weighted nature. Unlike AWT, Java Swing provides platform-independent and lightweight components.

The javax.swing package provides classes for java swing API such as JButton, JTextField, JTextArea, JRadioButton, JCheckbox, JMenu, JColorChooser etc.

Java Swing Example

We can write the code of swing inside the main(), constructor or any other method.

Let’s see a simple swing example where we are creating one button and adding it on the JFrame object inside the main() method.

import javax.swing.*; 

public class FirstSwingExample { 

public static void main(String[] args) { 

JFrame f=new JFrame();//creating instance of JFrame 


JButton b=new JButton(“click”);//creating instance of JButton 

b.setBounds(130,100,100, 40);//x axis, y axis, width, height 


f.add(b);//adding button in JFrame 


f.setSize(400,500);//400 width and 500 height 

f.setLayout(null);//using no layout managers 

f.setVisible(true);//making the frame visible 



Swing components:

Below are the some components of swing in java:

1. ImageIcon

The ImageIcon component creates an icon sized-image from an image residing at the source URL.


ImageIcon homeIcon = new ImageIcon(“src/images/home.jpg”);

2. JButton

JButton class is used to create a push-button on the UI.


JButton okBtn = new JButton(“Ok”);


3. JLabel

JLabel class is used to render a read-only text label or images on the UI. It does not generate any event.



JLabel textLbl = new JLabel(“This is a text label.”);

4. JTextField

JTextField renders an editable single-line text box. A user can input non-formatted text in the box. To initialize the text field, call its constructor and pass an optional integer parameter to it. This parameter sets the width of the box measured by the number of columns. It does not limit the number of characters that can be input in the box.


JTextField txtBox = new JTextField(20);

It renders a text box of 20 column width.

5. JTextArea

JTextArea class renders a multi-line text box. Similar to the JTextField, a user can input non-formatted text in the field.


JTextArea txtArea = new JTextArea(“This text is default text for text area.”, 5, 20);

The above code renders a multi-line text-area of height 5 rows and width 20 columns, with default text initialized in the text-area.



Leave a Comment