Home »
Java »
Java Articles
Java - Difference Between AWT and Swing
By Shahnail Khan Last updated : March 14, 2024
To create graphical user interfaces (GUIs) in Java, developers have two main frameworks: AWT (Abstract Window Toolkit) and Swing. Both serve the purpose of creating GUI components in Java applications, but they have some fundamental differences. Let's understand these frameworks and know the better choice for your Java projects.
What is AWT?
AWT is stands for Abstract Window Toolkit, it is one of the earliest GUI frameworks in Java. It provides a set of classes for building GUI components such as buttons, text fields, and windows. AWT components are native platform components, which means that they rely on the underlying operating system's GUI libraries for rendering.
AWT Example
import java.awt.*;
public class Main extends Frame {
public Main() {
Button button = new Button("Click me");
add(button);
setSize(300, 200);
setVisible(true);
}
public static void main(String[] args) {
new Main();
}
}
Explanation
- The code imports the necessary AWT (Abstract Window Toolkit) classes to create graphical user interfaces (GUIs) in Java.
- The Main class extends the Frame class from AWT, which provides the basic functionality for creating windows.
- Inside the constructor of the Main class, a button labelled "Click me" is created using the Button class from AWT.
- The button is added to the window (Frame) using the add() method, which allows components to be placed inside the window.
- Finally, the size of the window is set to 300 pixels wide and 200 pixels tall, and the window is made visible using the setSize() and setVisible(true) methods, respectively.
What is Swing?
Swing is an extension of AWT and provides a more sophisticated set of GUI components. Swing components are not dependent on the underlying operating system's GUI libraries. Swing components are entirely written in Java and offer a consistent look and feel across different platforms.
Swing Example
import javax.swing.*;
public class SwingExample {
public static void main(String[] args) {
JFrame frame = new JFrame("Swing Example");
JButton button = new JButton("Click me");
frame.getContentPane().add(button);
frame.setSize(300, 200);
frame.setVisible(true);
}
}
Explanation
- Firstly, we import the javax.swing.* package, which provides classes for creating GUI components in Java Swing, a more advanced GUI framework than AWT.
- The SwingExample class contains the main method. Inside the main method, a JFrame object named frame is created. JFrame is a top-level container that represents a window in Swing.
- A JButton named button is created with the label "Click me". JButton represents a clickable button component in Swing.
- The button is added to the content pane of the frame using the getContentPane().add(button) method. Then, the size of the frame is set to 300 pixels wide and 200 pixels tall, and the frame is made visible on the screen using frame.setSize(300, 200) and frame.setVisible(true) respectively.
- This results in a window named "Swing Example" with a button labelled "Click me" displayed inside it.
Difference between AWT and Swing in Java
The below table shows the differences between AWT and Swing in Java:
Feature |
Java AWT |
Java Swing |
Dependence |
Dependent on native platform components. |
Independent of native platform components. |
Look and feel
|
Gives the look and feel of the OS. |
Provides a consistent look and feel. |
Performance
|
Potentially faster due to native code. |
Slightly slower due to pure Java implementation. |
Complexity
|
Simpler, with fewer components and features. |
More complex, with extensive components and features. |