AWT in Java: AWT is a Java toolkit used for making powerful interfaces for Java applications. The full form of AWT in Java is Abstract Window Toolkit. It provides a set of classes and methods used to develop windows, buttons, menus, dialog boxes, and other major GUI components. Let us know more about AWT in Java and its uses in this article.
What is AWT in Java?
Java consists of many predefined methods and frameworks, which help to make development smooth and easier. AWT in Java is a set of APIs used to make useful graphical user interface applications in Java. It is one of the older frameworks in Java. It consists of many components such as checkboxes, radio buttons, text fields, etc frequently used in applications.
It can handle user input easily and integrate with the native platforms to run smoothly. This toolkit can be imported into your Java code from java.awt package. It contains classes required to create window basd Java programs for Java applications. AWT use resources of operating system on which it is working. Its components are heavyweight.
Also Read: How to Get Input From Users in Java?
AWT in Java: Important Highlights
Let us dive into the important highlights of AWT in Java below.Â
- AWT in Java can be imported using import using java.awt package.Â
- AWT stands for Abstract Window Toolkit for creating Graphical User Interface (GUI) for different Java applications.
- The components of AWT in Java are component, container, window, frame, dialog, canvas, panel, image, menu component, font, colors, etc.
- They also manages different events such as click, hover, and other event listeners, etc.
- They arrange differnent components in a proper layout with proper sizing and positioning.
- They can also handle colors and fonts which help developers to customize appearance of their Java applications easily.
- With the help of java.awt.graphics developers can easily draw different shapes such as Images, text and more.
- It provides containers such as Panel, frames, and dialog to organize components in a proper layout.
AWT in Java: Hierarchy
The AWT components in Java are given proper order and preferences. Check the elements given below according to the hierarchy of AWT in java.
- Components: Components consist of Buttons, choice, list, checkbox, input, label, list, etc which is used to make GUI elements interactive in Java.
- Containers: They are the second element in the AWT library which consist s of panel, frames and dialogues which is used to organize and give proper layout to components of AWT in Java applications.
- Layout Managers: They consit of Flow Layout, Border Layout, etc to arrange data inside the containers in proper space.
- Event Handling: AWT can handle events such as mouse hovers, clicks, etc to respond with a particular event listener functions.
- Graphics: With the help of java.awt.graphics developers can create different shapes, insert images and write texts inside the components.
Read More: Armstrong Number In Java: Everthing You Need to Know
AWT in Java: Are they Platform Independent?
Abstract Window ToolKits in Java are API tools used to provide various graphic interface tools for Java applications. AWT quickly adapts the platforms it is currently running on to adjust the layouts and appearance as the platform view forms. It can create uniform interfaces on different platforms, such as Windows, MacOS, Linux and other operating systems.
The components of AWT are not independent of the platform, but they can quickly integrate with the system OS to adjust their layout appearance. They can make it possible because Java applications connect with AWT using abstract APIs, which are platform-independent. These abstract APIs help AWT generate platform details and make codes portable on various platforms.Â
Also, the AWS libraries are written in Java language, which itself is platform-independent. Hence, due to these reasons, AWT in Java is platform independent.
Recommended Technical CourseÂ
- Full Stack Development Course
- Generative AI Course
- DSA C++ Course
- Data Analytics Course
- Python DSA Course
- DSA Java Course
AWT in Java: Examples
Let us understand the implementation of AWT in Java using various elements such as Frame, Components, layout manager, event handling, etc.Â
AWT in Java: Example |
import java.awt.*;
import java.awt.event.*; public class AWT_implement{     public static void main(String[] args) {         // Create a frame (window)         Frame frame = new Frame(“AWT implementation”);         // Create a label         Label label = new Label(“Hello, AWT!”);         // Create a button         Button button = new Button(“Click Me”);         // Set layout manager for the frame         frame.setLayout(new FlowLayout());         // Add the label and button to the frame         frame.add(label);         frame.add(button);         // Handle button click using an anonymous inner class         button.addActionListener(new ActionListener() {             public void actionPerformed(ActionEvent e) {                 label.setText(“Button Clicked!”);             }         });         // Handle window close event using WindowAdapter         frame.addWindowListener(new WindowAdapter() {             public void windowClosing(WindowEvent e) {                 System.exit(0);             }         });         // Set frame size and make it visible         frame.setSize(300, 150);         frame.setVisible(true);     } } |
First, import the awt package using import java.awt*. Now, first, create a frame instance and then create the labels and buttons required on your applications. Set the layout using the FlowLayout method. Now, add components to frame and handle different events using the event listeners. You can also modify the size and differentiate the visibility of elements.
AWT in Java: Components and Toolkits
AWT in Java consists of many methods. Check some of the methods in AWT below.
AWT in Java: Methods | ||
AWT toolkits | Syntax | Constructors/ Methods |
AWT Label | Public class Label extends Component implements Accessible | Label();
Label(String str)
Label (String str, int a) |
AWT TextField | Public class TextField extends TextComponent | TextField()
TextField(String text)
TextField(int col)
TextField(String str, int col) |
AWT Button | Public class TextField extends Component implements Accessible | Button()
Button(String str) |
AWT Checkbox | Public class Checkbox extends Component implements ItemSelectable, Accessible | Checkbox()
Checkbox(String str)
Checkbox(String str, boolean state, CheckboxGroup group) |
AWT Choice | Public class Choice extends Component implements ItemSelectable, Accessible | Choice() |
AWT List | Public class List extends Component implements ItemSelectable, Accessible | List()
List(int row)
List (int row, Boolean Mode) |
AWT Canvas | Public class Canvas extends Component implements | Canvas(); |
Action Listener | Public class ActionListenerExample Implements ActionListener | actionPerformed() |
AWT Scrollbar | Public class Scrollbar extends Component implements Adjustable, Accessible | Scrollbar() |
Java Mouse Listener | mouseClicked(MouseEvent x)
mousePressed(MouseEvent x)
mouseEntered(MouseEvent x)
mouseExited(MouseEvent x) |
|
Java ItemListener | itemStateChanged() | |
Java MouseMotion Listener | mouseDragged(MouseEvent x)
mouseMoved(MouseEvent x) |
|
Java KeyListener | Public interface KeyListener extends EventListener | keyPressed (KeyEvent x)
keyReleased(KeyEvent x)
keyTyped (KeyEvent x) |
Java WindowListener | Public interface WindowListener extends EventListener | windowActivated (WindowEvent x)
windowClosed (WindowEvent x)
windowClosing (WindowEvent x) |
AWT in Java: Examples to create Buttons using AWT toolkit
Check the implementation of AWT toolkit to create button in Java applications.
AWT in Java: Examples |
import java.awt.*;
import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; public class MultipleButtonsExample {     public static void main(String[] args) {         Frame frame = new Frame(“Simple Buttons Example”);         Button button1 = new Button(“Button 1”);         button1.setBounds(50, 100, 80, 30);         // Adding action listeners to handle button clicks         button1.addActionListener(new ActionListener() {             @Override             public void actionPerformed(ActionEvent e) {                 System.out.println(“Button 1 clicked”);             }         });         frame.add(button1);         frame.setSize(400, 200);         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);             }         });     } } |
Output
The simple output button looks like this using AWT toolkit and event listeners. However, we can add more styles and modifications to make it look attractive.
Learn Java Language with PW SkillsÂ
Join our Decode Java with DSA Course to learn Java language with data structures and algorithms. The course is effectively planned to prepare for job roles such as Java developers, Software engineers, Software developers, etc.Â
Learn with more than seven real time industry level projects. You will be provided with a course completion certificate which is recognised by top IT companies. Along with the course, you will also get a hundred percent placement assistance. Hurry! Join only at @pwskills.com
For Latest Tech Related Information, Join Our Official Free Telegram Group : PW Skills Telegram Group
AWT in Java FAQs
What is AWT in Java?
AWT in Java is a set of APIs used to make useful graphical user interface applications in Java. It is one of the older frameworks in Java. It consists of many components such as checkboxes, radio buttons, text fields, etc, frequently used in applications.
How to import the AWT toolkit in Java?
AWT in Java can be easily imported from java.awt package.
Is AWT on the Java platform independent?
The components of AWT are not independent of the platform, but they can quickly integrate with the system OS to adjust their layout appearance. AWS libraries are written in the Java language, which itself is platform-independent. Hence, AWT in Java is platform-independent.
Is AWT in Java still in use?
AWT in Java consists of overhead and heavyweight components that use system resources in which they are currently running. Developers now have a better string with a swing.