In today’s GUI-centric world it is expected that java program would let a program interact with user using GUI elements like text boxes, push button, radio buttons, check boxes, etc. To facilitate this interaction java provides two libraries Active Window Toolkit (AWT) and Swing.
In this blog, we will explore the concept of AWT and Swing and some of its GUI based applications.
What is AWT?
Abstract Window Toolkit (AWT) is Java's original platform-independent GUI toolkit. Introduced in Java 1.0, it provides a set of lightweight components for building user interfaces. AWT is tightly coupled with the native platform’s GUI resources, meaning it relies on the operating system's native components, such as buttons and windows.
Advantages of AWT:-
Platform Independence:- AWT applications can run on any platform with a JVM, while still leveraging native components.
Lightweight API:- The library is simpler compared to Swing, making it suitable for small applications.
Disadvantages of AWT:-
Limited Components:- AWT has fewer GUI components compared to modern frameworks.
Poor Extensibility:- Customization options for AWT components are limited.
Here’s and example of simple AWT application:-
import java.awt.*;
public class AWTExample {
public static void main(String[] args) {
Frame frame = new Frame("AWT Example");
Button button = new Button("Click Me");
button.setBounds(50, 100, 80, 30);
frame.add(button);
frame.setSize(300, 200);
frame.setLayout(null);
frame.setVisible(true);
// Close window event
frame.addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent windowEvent) {
System.exit(0);
}
});
}
}
Drawing a Rectangle with AWT
import java.awt.*;
public class RectangleAWTExample extends Frame {
public RectangleAWTExample() {
setSize(400, 400); // Set the frame size
setVisible(true); // Make the frame visible
// Add a window listener to close the application
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent windowEvent) {
System.exit(0);
}
});
}
public void paint(Graphics g) {
g.setColor(Color.BLUE); // Set the color to blue
g.drawRect(50, 50, 200, 100); // Draw an outline of a rectangle (x, y, width, height)
g.fillRect(100, 200, 150, 80); // Draw a filled rectangle (x, y, width, height)
}
public static void main(String[] args) {
new RectangleAWTExample(); // Create and display the frame
}
}
Explanation:-
drawRect(int x, int y, int width, int height)
draws the outline of a rectangle.fillRect(int x, int y, int width, int height)
draws a filled rectanglepaint(Graphics g)
is overridden to specify custom drawing on the frame.
What is Swing?
Swing, introduced in Java 1.2 as part of the Java Foundation Classes (JFC), builds on top of AWT. Unlike AWT, Swing is entirely written in Java and uses lightweight components. This allows for a consistent look and feel across all platforms.
Advantages of Swing:-
Rich Components:- Swing provides advanced components like tables (
JTable
), trees (JTree
), and panes (JTabbedPane
).Customizable:- Developers can easily customize the look and feel of Swing applications using pluggable Look and Feel (PLAF).
Platform Independence:- Swing components are not preferred to the native OS, they render the same on all platforms.
Extensibility:- Swing provides greater flexibility for creating custom components.
Disadvantages of Swing:-
Heavyweight API:- Swing’s components are generally heavier that AWT which can lead to slower performance.
Complex API:- Swing’s rich feature set comes with increased complexity for beginners.
Here’s an example of a simple Swing application:-
import javax.swing.*;
public class SwingExample {
public static void main(String[] args) {
JFrame frame = new JFrame("Swing Example");
JButton button = new JButton("Click Me");
button.setBounds(50, 100, 100, 40);
frame.add(button);
frame.setSize(300, 200);
frame.setLayout(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
Drawing a Rectangle with Swing
import javax.swing.*;
import java.awt.*;
public class RectangleSwingExample extends JPanel {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g); // Call the parent class's paintComponent method
g.setColor(Color.RED); // Set the color to red
g.drawRect(50, 50, 200, 100); // Draw an outline of a rectangle (x, y, width, height)
g.fillRect(100, 200, 150, 80); // Draw a filled rectangle (x, y, width, height)
}
public static void main(String[] args) {
JFrame frame = new JFrame("Rectangle Swing Example");
RectangleSwingExample panel = new RectangleSwingExample();
frame.add(panel); // Add the custom panel to the frame
frame.setSize(400, 400); // Set the frame size
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Exit the application on close
frame.setVisible(true); // Make the frame visible
}
}
Explanation:-
paintComponent(Graphics g)
is the preferred method for custom painting in Swing.super.paintComponent(g)
ensures the panel is properly rendered before drawing.You can use
g.drawRect()
andg.fillRect()
methods as in AWT.
When to Use AWT or Swing?
Use AWT for simple applications where performance is critical, and you don’t need advanced GUI components.
Use Swing for modern applications requiring rich, feature-complete GUIs with a consistent look and feel across platforms.
Conclusion
Both AWT and Swing have been foundational in Java GUI development, but they cater to different needs. While AWT is lightweight and closer to the native OS, Swing provides a richer set of tools for crafting modern applications. With the rise of JavaFX, however, both AWT and Swing are being gradually replaced in new applications. Still, understanding these libraries remains essential for maintaining and modernizing legacy Java projects.
Which framework do you prefer for GUI development, and why? Share your thoughts in the comments below!