GUI Tutorial in Java - AWT vs Swing

GUI stands for Graphical User Interface. Using GUI you can create GUI-based applications like Login Form, Registration Form etc.
Java provides two APIs to create User Interface:
- AWT (Abstract Window Toolkit)
- JFC (Java Foundation Classes) - Swing
Difference between AWT and JFC
| AWT | JFC (Swing) |
|---|---|
| AWT stands for Abstract Window Toolkit | JFC stands for Java Foundation Classes |
| AWT components are Windows components (native peers) | JFC components are Java components (pure Java) |
Exists in java.awt package | Exists in javax.swing package |
| Not Platform Independent - looks different on each OS | Platform Independent - same look on all OS |
| Heavyweight components | Lightweight components |
| Limited classes - around 300 | Rich API - around 2000 classes |
| Does not support pluggable look and feel | Supports pluggable look and feel |
| Provides event and listener classes (AWT event model) | Uses AWT event model + supports MVC |
| Does not support MVC (Model-View-Controller) | Supports MVC Architecture |
Hierarchy of AWT Components

What is Component?
Components are basic building blocks of a GUI application. Component is a root class for all GUI components.
Components are divided into two forms:
- Container
- Components
1. Container
Container provides a space where components like button, textfield, radiobutton, textarea etc. can be placed.
Container is used to hold group of components. In Java every Container has its own default layout like BorderLayout, FlowLayout etc.
Java Swing provides following Containers:
- JWindow
- JFrame
- JPanel
- JDesktopPane
- JInternalFrame
- JDialog
2. Components
A component is an independent visual control placed inside a container. Some important Swing components:
- JOptionPane, JTextField, JLabel, JPasswordField, JTextArea
- JRadioButton, JCheckBox, JComboBox, JSpinner
- JProgressBar, JScrollPane, JList, JButton
- JTable, JSplitPane, JTree, JFileChooser, JColorChooser
- JMenuBar, JMenu, JMenuItem, JPopupMenu, JToolBar
- JToggleButton, JEditorPane, JSlider, JSeparator
What is Layout?
Layout means arrangement of components within the container. Layout is part of container. Each container has a default layout.
Types of Layouts:
- BorderLayout
- FlowLayout
- GridLayout
1. BorderLayout
BorderLayout adds components in five directions: NORTH, SOUTH, EAST, WEST, CENTER.
Default layout of JWindow, JFrame, JDesktopPane, JDialog and JInternalFrame. If direction is not specified, it adds to CENTER.
import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Example {
private JButton b1, b2, b3, b4, b5;
public Example() { initComponents(); }
private void initComponents() {
b1 = new JButton("EAST");
b2 = new JButton("WEST");
b3 = new JButton("NORTH");
b4 = new JButton("SOUTH");
b5 = new JButton("CENTER");
JFrame f = new JFrame("Border Layout Example");
f.setSize(300, 300);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLocationRelativeTo(null);
f.add(b1, BorderLayout.EAST);
f.add(b2, BorderLayout.WEST);
f.add(b3, BorderLayout.NORTH);
f.add(b4, BorderLayout.SOUTH);
f.add(b5, BorderLayout.CENTER);
f.setVisible(true);
}
public static void main(String[] args) { new Example(); }
}
2. FlowLayout
FlowLayout is used to arrange components in a sequence. Default layout of JPanel.
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class FlowLayoutDemo {
private JButton b1, b2, b3, b4, b5;
public FlowLayoutDemo() { initComponents(); }
private void initComponents() {
b1 = new JButton("EAST"); b2 = new JButton("WEST"); b3 = new JButton("NORTH");
b4 = new JButton("SOUTH"); b5 = new JButton("CENTER");
JFrame f = new JFrame("Flow Layout Example");
f.setSize(400, 300);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLocationRelativeTo(null);
JPanel p = new JPanel();
FlowLayout fl = (FlowLayout) p.getLayout();
fl.setAlignment(FlowLayout.CENTER); // LEFT, RIGHT, LEADING, TRAILING, CENTER
p.add(b1); p.add(b2); p.add(b3); p.add(b4); p.add(b5);
f.add(p);
f.setVisible(true);
}
public static void main(String[] args) { new FlowLayoutDemo(); }
}
3. GridLayout
GridLayout is used to arrange components in grid format based on rows and columns.
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
public class GridLayoutExample {
private JButton b1, b2, b3, b4, b5;
public GridLayoutExample() { initComponents(); }
private void initComponents() {
b1 = new JButton("EAST"); b2 = new JButton("WEST"); b3 = new JButton("NORTH");
b4 = new JButton("SOUTH"); b5 = new JButton("CENTER");
JFrame f = new JFrame("Grid Layout Example");
f.setSize(300, 300);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLocationRelativeTo(null);
f.setLayout(new GridLayout(2, 3)); // 2 rows, 3 cols
f.add(b1); f.add(b2); f.add(b3); f.add(b4); f.add(b5);
f.setVisible(true);
}
public static void main(String[] args) { new GridLayoutExample(); }
}
Conclusion
For modern GUI development, always prefer Swing (JFC) over AWT because it is platform independent, lightweight, and supports pluggable look and feel and MVC. Master BorderLayout, FlowLayout and GridLayout to create professional desktop applications.
No comments:
Post a Comment