GUI in Java - AWT vs Swing (JFC)

GUI in Java - AWT vs Swing (JFC), Components & Layouts Explained (2026)

GUI Tutorial in Java - AWT vs Swing

GUI Tutorial Java

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:

  1. AWT (Abstract Window Toolkit)
  2. JFC (Java Foundation Classes) - Swing

Difference between AWT and JFC

AWTJFC (Swing)
AWT stands for Abstract Window ToolkitJFC stands for Java Foundation Classes
AWT components are Windows components (native peers)JFC components are Java components (pure Java)
Exists in java.awt packageExists in javax.swing package
Not Platform Independent - looks different on each OSPlatform Independent - same look on all OS
Heavyweight componentsLightweight components
Limited classes - around 300Rich API - around 2000 classes
Does not support pluggable look and feelSupports 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

AWT Component Hierarchy

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:

  1. Container
  2. 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:

  1. JWindow
  2. JFrame
  3. JPanel
  4. JDesktopPane
  5. JInternalFrame
  6. JDialog

2. Components

A component is an independent visual control placed inside a container. Some important Swing components:

  1. JOptionPane, JTextField, JLabel, JPasswordField, JTextArea
  2. JRadioButton, JCheckBox, JComboBox, JSpinner
  3. JProgressBar, JScrollPane, JList, JButton
  4. JTable, JSplitPane, JTree, JFileChooser, JColorChooser
  5. JMenuBar, JMenu, JMenuItem, JPopupMenu, JToolBar
  6. 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:

  1. BorderLayout
  2. FlowLayout
  3. 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.

BorderLayout Example
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.

FlowLayout Example
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.

GridLayout Example
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: