AWT
AWT
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.
In this AWT tutorial, you will learn the basics of the AWT, including how to create windows, buttons,
labels, and text fields. We will also learn how to add event listeners to components so that they can
respond to user input.
By the end of this tutorial, you will have a good understanding of the AWT and be able to create simple
GUIs in Java.
• Abstract APIs:
AWT provides an abstract layer for GUI. Java applications interact with AWT through Abstract API
which are platform independent. Abstract API allows Java to isolate platform-specific details, making
code portable across different systems.
• Platform-Independent Libraries:
The Libraries of AWT are written in Java which they are totally platform-independent. Because of this, it
ensures that AWT functionality remains consistent across different environments.
Java AWT Hierarchy
Java-AWT-Tutorial-Large
Components: AWT provides various components such as buttons, labels, text fields, checkboxes, etc used
for creating GUI elements for Java Applications.
Containers: AWT provides containers like panels, frames, and dialogues to organize and group
components in the Application.
Layout Managers: Layout Managers are responsible for arranging data in the containers some of the
layout managers are BorderLayout, FlowLayout, etc.
Event Handling: AWT allows the user to handle the events like mouse clicks, key presses, etc. Using
event listeners and adapters.
Graphics and Drawing: It is the feature of AWT that helps to draw shapes, insert images and write text in
the components of a Java Application.
Note: Container can be added inside another container as it is type of component.
Types of Containers in Java AWT
There are four types of containers in Java AWT:
Window: Window is a top-level container that represents a graphical window or dialog box. The Window
class extends the Container class, which means it can contain other components, such as buttons, labels,
and text fields.
Panel: Panel is a container class in Java. It is a lightweight container that can be used for grouping other
components together within a window or a frame.
Frame: The Frame is the container that contains the title bar and border and can have menu bars.
Dialog: A dialog box is a temporary window an application creates to retrieve user input.
Java AWT Tutorial for Beginner & Experienced
Learn the basics of the Abstract Window Toolkit (AWT) in Java, for both beginners and experienced
developers.
• Button( ):
Creates a Button with no label i.e. showing an empty box as a button.
• Button(String str):
Creates a Button with String str as a label. For example if str=”Click Here” button with show click here as
the value.
Java AWT TextField
Syntax of AWT TextField:
Public class TextField extends TextComponent
TextField Class constructors
There are TextField class constructors are mentioned below:
1. TextField():
Constructs a TextField component.
2. TextField(String text):
Constructs a new text field initialized with the given string str to be displayed.
3. TextField(int col):
Creates a new text field(empty) with the given number of columns (col).
4. TextField(String str, int columns):
Creates a new text field(with String str in the display) with the given number of columns (col).
5. Java AWT Checkbox
Syntax of AWT Checkbox:
Public class Checkbox extends Component implements ItemSelectable, Accessible
Checkbox Class Constructors
There are certain constructors in the AWT Checkbox class as mentioned below:
1. Checkbox():
Creates a checkbox with no label.
2. Checkbox(String str):
Creates a checkbox with a str label.
3. Checkbox(String str, boolean state, CheckboxGroup group):
Creates a checkbox with the str label, and sets the state in the mentioned group.
4. Java AWT CheckboxGroup
CheckboxGroup Class is used to group together a set of Checkbox.
Syntax of AWT CheckboxGroup:
Public class CheckboxGroup extends Object implements Serializable
Note: CheckboxGroup enables the use of radio buttons in AWT.
Java AWT Choice
The object of the Choice class is used to show a popup menu of choices.
Syntax of AWT Choice:
Public class Choice extends Component implements ItemSelectable, Accessible
AWT Choice Class constructor
Choice(): It creates a new choice menu.
Java AWT List
The object of the AWT List class represents a list of text items.
Syntax of Java AWT List:
Public class List extends Component implements ItemSelectable, Accessible
AWT List Class Constructors
The List of class constructors is defined below:
1. List():
Creates a new list.
2. List(int row):
Creates lists for a given number of rows(row).
3. List(int row, Boolean Mode)
Ceates new list initialized that displays the given number of rows.
Java AWT Canvas
Syntax of AWT Canvas:
Public class Canvas extends Component implements Accessible
Canvas Class Constructors
1. Canvas():
Creates new Canvas.
2. Canvas(GraphicConfiguration config):
It creates a new Canvas with the given Graphic configuration.
AWT Scrollbar
Syntax of AWT Scrollbar:
Public class Scrollbar extends Component implements Adjustable, Accessible
Java AWT Scrollbar Class Constructors
There are three constructor classes in Java mentioned below:
1. Scrollbar():
It Creates a new vertical Scrollbar in the Application.
2. Scrollbar(int orientation):
Creates a new vertical Scrollbar with the given orientation.
3. Scrollbar(int orientation, int value, int visible, int mini, int maxi):
Creates a new scrollbar with the orientation mentioned with value as the default value and [mini, maxi]
as the lower and higher limit.
Java AWT MenuItem & Menu
MenuItem class adds a simple labeled menu item on the menu. The MenuItem class allows you to create
individual items that can be added to menus. And Menu is a component used to create a dropdown menu
that can contain a list of MenuItem components.
Syntax of Java AWT MenuItem
Public class MenuItem extends MenuComponent implements Accessible
Syntax of Java AWT Menu
Public class Menu extends MenuItem implements MenuContainer, Accessible
Java AWT PopupMenu is a component that is used for dynamically popping up a menu that appears when
the user right-clicks or performs any other action on a component.
Syntax of AWT PopupMenu
Public class PopupMenu extends Menu implements MenuContainer, Accessible
Java AWT Panel
Java AWT Panel is a container class used to hold and organize graphical components in a Java
Application.
Syntax of Java AWT Panel:
Public class Panel extends Container implements Accessible
Java AWT Toolkit
Java AWT Toolkit class provides us with a platform-independent way to access various system resources
and functionalities. Subclasses of Toolkit are used to bind various components.
Syntax of Java AWT Toolkit
Public abstract class Toolkit extends Object
Event Handling Components – Java AWT
Here are some of the event handling components in Java:
• Java ActionListener
• Java MouseListener
• Java MouseMotionListener
• Java ItemListener
• Java KeyListener
• Java WindowListener
• Close AWT Window
Java ActionListener
Java ActionListner is a interface which responds to the actions performed by the components like buttons,
menu items ,etc.
Syntax of Java ActionListener:
Public class ActionListenerExample Implements ActionListener
There is only methods associated with ActionListner class that is actionPerformed().
Syntax of actionPerformed() method:
Public abstract void actionPerformed(ActionEvent e);
Java MouseListener
Java MouseListner is a interface that responds to the actions performed by mouse events generated by the
user. Example: mouse clicks , mouse movements, etc.
There are 5 Methods associated with MouseListner:
1. mouseClicked(MouseEvent e):
Responds to mouse buttons when clicked on a component in the Application.
2. mousePressed(MouseEvent e):
Responds to mouse button is Pressed on a component in the Application.
3. mouseReleased(MouseEvent e):
Responds to Mouse button released after being pressed over a component in the Application.
// Java AWT Program for Button
Import java.awt.*;
Import java.awt.event.WindowAdapter;
Import java.awt.event.WindowEvent;
// Driver Class
Public class Button_Example {
// main function
Public static void main(String[] args)
{
// Creating instance of frame with the label
Frame frame = new Frame(“Example 2”);
// Creating instance of button with label
Button button = new Button(“Click Here”);
// Setting the position for the button in frame
Button.setBounds(80, 100, 64, 30);
// Adding button to the frame
Frame.add(button);
// setting size, layout and visibility of frame
Frame.setSize(300, 300);
Frame.setLayout(null);
Frame.setVisible(true);
// Using WindowListener for closing the window
Frame.addWindowListener(new WindowAdapter() {
@Override
Public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
}
4. mouseEntered(MouseEvent e):
Responds to the situation when a Mouse cursor enters the bounds of a component in an Application.
5. mouseExited(MouseEvent e):
Responds to the situation when a Mouse cursor exits a component’s bounds.
Java MouseMotionListener
Java MouseMotionListner is a interface which is notified when mouse is moved or dragged.
It contains two Methods mentioned below:
1. mouseDragged(MouseEvent e):
Responds when the mouse is dragged with mouse button clicked over a component in Application.
2. mouseMoved(MouseEvent e):
Responds when the mouse is moved over a component in Application.
Java ItemListener
Java ItemListner is an interface which handles events related to item selection and deselection those that
occur with checkboxes, radio buttons, etc. There is only one Method associated with ItemListner that is
itemStateChanged(). This method provides information about the event, i.e. source of the event and the
changed state.
Syntax of itemStateChanged() method:
itemStateChanged(ItemEvent e)
Java KeyListener
Java KeyListner is an interface in Java notified whenever you change the state of key or can be said for
key related events.
Syntax of KeyListener:
Public interface KeyListener extends EventListener
There are three methods associated with KeyListner as mentioned below:
1. keyPressed (KeyEvent e):
Responds to the event when key is pressed.
2. keyReleased (KeyEvent e):
Responds to the event when the key is released.
3. keyTyped (KeyEvent e):
Responds to the key has been typed.
Java WindowListener
Java WindowListener is a interface used for handling events related to window actions. Events like
opening , closing, minimizing, etc are handled using WindowListener.
Syntax of WindowListener
Public interface WindowListener extends EventListener
There are seven methods associated with WindowListener as mentioned below:
1. windowActivated (WindowEvent e):
Responds when window is first opened
2. windowClosed (WindowEvent e):
Responds when the user attempts to close the window
3. windowClosing (WindowEvent e):
Responds after a window has been closed
4. windowDeactivated (WindowEvent e):
Responds when a window is minimized
5. windowDeiconified (WindowEvent e):
Responds when a window is restored from a minimized state
6. windowIconified (WindowEvent e):
Responds when a window is activated
7. windowOpened (WindowEvent e):
Responds when a window loses focus.
Java Adapter classes
Java adapter classes provide the default implementation of listener interfaces.
Close AWT Window
At the end we will need to Close AWT Window, So to perform this task we will use dispose() method.
This method releases the resources associated with the window and also removes it from the screen.
Java AWT Examples
Hello World in Java AWT
Hello, World is was the first step in learning Java. So, let us program our first Program in Java AWT as
Hello World using Labels and Frames.
Below is the implementation of the above method:
// Java AWT Program for Hello World
Import java.awt.*;
Import java.awt.event.WindowAdapter;
Import java.awt.event.WindowEvent;
// Driver Class
Public class AWT_Example {
// main function
Public static void main(String[] args)
{
// Declaring a Frame and Label
Frame frame = new Frame(“Basic Program”);
Label label = new Label(“Hello World!”);
// Aligning the label to CENTER
Label.setAlignment(Label.CENTER);
// Adding Label and Setting
// the Size of the Frame
Frame.add(label);
Frame.setSize(300, 300);
// Making the Frame visible
Frame.setVisible(true);
// Using WindowListener for closing the window
Frame.addWindowListener(new WindowAdapter() {
@Override
Public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
}
Running:
Javac AWT_Example.java
Java AWT_Example
Output:
Java AWT Hello World
Java AWT Program to create Button
Below is the implementation of the Java AWT Program to create a Button:
// Java AWT Program for Button
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
// Driver Class
public class Button_Example {
// main function
public static void main(String[] args)
{
// Creating instance of frame with the label
Frame frame = new Frame("Example 2");
// Creating instance of button with label
Button button = new Button("Click Here");
// Setting the position for the button in frame
button.setBounds(80, 100, 64, 30);
// Adding button to the frame
frame.add(button);
// setting size, layout and visibility of frame
frame.setSize(300, 300);
frame.setLayout(null);
frame.setVisible(true);
// Using WindowListener for closing the window
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
}
Running:
Javac Button_Example.java
Java Button_Example
Output:
Java AWT Button Output
Java AWT Tutorial – FAQs
1. What is AWT in Java?
AWT stands for Abstract Window Toolkit, which is an API for creating graphical user interface (GUI) or
windows-based applications in Java. AWT provides various components like buttons, labels, text fields,
etc. That can be used as objects in a Java program.