AWT, Swing, and Applet
Before modern web and mobile interfaces became dominant, Java was widely used for creating desktop graphical applications. For this purpose, Java introduced GUI programming libraries such as AWT and Swing. Java also introduced Applet, which allowed small Java programs to run inside web browsers.
Even though Applets are now outdated and no longer used in modern browsers, AWT and Swing are still important from a learning and interview perspective because they explain the foundation of Java GUI programming.
What is GUI Programming?
GUI stands for Graphical User Interface. It allows users to interact with a program using visual elements like:
- windows
- buttons
- menus
- text fields
- labels
- checkboxes
Instead of typing only commands in the console, users can interact with visual components.
What is AWT?
AWT stands for Abstract Window Toolkit. It is Java’s original GUI toolkit used for building window-based applications.
It provides classes for creating components like:
- Frame
- Button
- Label
- TextField
- Checkbox
- Menu
AWT belongs mainly to the package:
Why It is Called Abstract Window Toolkit
AWT uses the operating system’s native GUI components. That means Java asks the underlying OS to create actual buttons, windows, and other elements.
Because of this:
- AWT components are platform-dependent
- their appearance may differ across operating systems
Basic AWT Components
Some common AWT classes are:
Frame→ main windowLabel→ text labelButton→ clickable buttonTextField→ single-line text boxTextArea→ multi-line text boxCheckbox→ checkbox optionChoice→ dropdown menu
AWT Example: Simple Frame
Here:
Framecreates a windowsetSize()sets the window sizesetVisible(true)makes the window appear
AWT Example with Button and Label
This creates a window with a label and a button.
Limitations of AWT
- platform-dependent look and feel
- limited set of components
- less flexible compared to Swing
- native component dependency
What is Swing?
Swing is a more advanced GUI toolkit built on top of AWT. It provides richer components and is mostly platform-independent in appearance.
Swing classes belong mainly to:
Swing improves AWT by providing:
- more components
- better customization
- lightweight components
- consistent look across platforms
Why Swing is Better than AWT
- more powerful and flexible
- supports richer GUI components
- less dependent on native OS rendering
- better user experience
Examples of Swing Components
JFrameJLabelJButtonJTextFieldJTextAreaJCheckBoxJRadioButtonJTableJMenuBarJOptionPane
AWT vs Swing Component Names
| AWT | Swing |
|---|---|
| Frame | JFrame |
| Label | JLabel |
| Button | JButton |
| TextField | JTextField |
| TextArea | JTextArea |
Basic Swing Example
This creates a simple Swing window.
Swing Example with Label and Button
This is similar to the AWT example but uses Swing components.
Event Handling in AWT and Swing
GUI applications respond to user actions such as button clicks, key presses, or mouse movement. This is called event handling.
Java uses event listeners for this.
Button Click Example in Swing
Here:
ActionListenerlistens for button click eventactionPerformed()runs when the button is clicked
Common Layout Managers
AWT and Swing use layout managers to arrange components inside containers.
Common layout managers:
FlowLayoutBorderLayoutGridLayoutCardLayoutBoxLayout
FlowLayout Example
Layout managers are preferred over fixed positions in most real GUI programs.
What is an Applet?
An Applet is a small Java program that was designed to run inside a web browser or applet viewer.
Applets were once used for:
- small web games
- animations
- interactive educational tools
- browser-based visual programs
However, modern browsers no longer support Java Applets due to security, plugin, and technology changes.
Applet Class
To create an applet, Java programs used to extend the Applet class or sometimes JApplet.
Basic package:
Simple Applet Example
This applet displays text inside the applet area.
Applet Lifecycle Methods
Applets had a specific lifecycle controlled by the browser or applet viewer.
init()start()paint(Graphics g)stop()destroy()
1. init()
Called once when the applet is first loaded.
2. start()
Called when the applet becomes active.
3. paint(Graphics g)
Used to draw text, shapes, and images.
4. stop()
Called when the applet is no longer active.
5. destroy()
Called when the applet is removed permanently.
Applet Lifecycle Example
Limitations of Applets
- browser plugin dependency
- security restrictions
- poor modern browser support
- obsolete technology in modern development
AWT vs Swing
| Point | AWT | Swing |
|---|---|---|
| Full form | Abstract Window Toolkit | Part of Java Foundation Classes |
| Component type | Heavyweight | Lightweight |
| Platform dependence | More platform-dependent | More platform-independent |
| Component richness | Limited | More advanced |
| Package | java.awt |
javax.swing |
AWT and Swing Relationship
Swing is built on top of AWT. That means Swing uses some AWT concepts and classes internally, especially for event handling and windowing support.
So:
- AWT is the older foundation
- Swing is the richer extension
Real-World Use Cases
- desktop forms
- simple educational tools
- old enterprise desktop applications
- custom utility software
In modern Java development, Swing is still sometimes used for desktop tools, but many new applications prefer JavaFX or web-based frontends.
Common Mistakes
- confusing AWT and Swing components
- mixing too many fixed bounds instead of using layout managers
- forgetting event handling for interactive components
- assuming Applets are still used in modern browsers
- not understanding that Swing is built over AWT
Best Practices
- prefer Swing over AWT for richer GUI applications
- use layout managers instead of hardcoded positioning when possible
- keep GUI code organized and readable
- use event listeners properly for user actions
- treat Applets as historical knowledge, not modern development choice
Interview-Oriented Points
- AWT is Java’s original GUI toolkit
- Swing is a more advanced GUI toolkit built on top of AWT
- AWT components are heavyweight and depend on native OS components
- Swing components are lightweight and more flexible
- Applet is a small Java program that used to run in browsers
- Applet lifecycle methods are
init(),start(),paint(),stop(), anddestroy() - Modern browsers no longer support Applets
Conclusion
AWT, Swing, and Applet are important parts of Java’s GUI history. AWT introduced basic graphical programming, Swing expanded it with richer and more flexible components, and Applets represented Java’s early browser-based execution model.
Even though Applets are obsolete now, understanding AWT and Swing still helps in learning desktop GUI concepts, event handling, and the evolution of Java UI development.

