Building GUI with Java Swing

📘 Java 👁 44 views 📅 Dec 01, 2025
⏱ Estimated reading time: 3 min

Java Swing is a lightweight GUI toolkit that allows developers to create graphical user interfaces in Java. It is part of the javax.swing package and provides rich, platform-independent GUI components.


1. Features of Java Swing

  • Lightweight components → Written entirely in Java, independent of OS

  • Rich set of components → Buttons, text fields, labels, tables, trees, etc.

  • Pluggable Look and Feel (L&F) → Can mimic native OS or custom themes

  • Event-driven programming → Responds to user actions like clicks, typing, etc.

  • Supports MVC architecture → Separates data (model) from UI (view)


2. Swing Components Overview

ComponentDescription
JFrameMain window/container
JPanelPanel to hold other components
JButtonClickable button
JLabelDisplay text or images
JTextFieldSingle-line text input
JTextAreaMulti-line text input
JCheckBoxSelectable box
JRadioButtonSelect one option from a group
JComboBoxDrop-down selection
JTableDisplay tabular data

3. Basic Steps to Build a Swing GUI

  1. Create a JFrame – main window

  2. Add Components – buttons, labels, text fields, etc.

  3. Set Layout Manager – control component arrangement (FlowLayout, BorderLayout, GridLayout)

  4. Add Event Listeners – handle user actions

  5. Make JFrame visible


4. Example: Simple Swing Application

import javax.swing.*; import java.awt.event.*; public class SimpleSwing { public static void main(String[] args) { // Create frame JFrame frame = new JFrame("Swing Example"); frame.setSize(400, 200); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Create panel JPanel panel = new JPanel(); frame.add(panel); placeComponents(panel); // Make frame visible frame.setVisible(true); } private static void placeComponents(JPanel panel) { panel.setLayout(null); // Label JLabel userLabel = new JLabel("Enter Name:"); userLabel.setBounds(10, 20, 100, 25); panel.add(userLabel); // Text Field JTextField userText = new JTextField(20); userText.setBounds(120, 20, 165, 25); panel.add(userText); // Button JButton loginButton = new JButton("Greet"); loginButton.setBounds(10, 60, 100, 25); panel.add(loginButton); // Event handling loginButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String name = userText.getText(); JOptionPane.showMessageDialog(panel, "Hello, " + name + "!"); } }); } }

Output:

  • Window with a label, text field, and button

  • On clicking the button, a greeting message pops up


5. Layout Managers in Swing

  • FlowLayout → Components arranged left to right

  • BorderLayout → Divides container into North, South, East, West, Center

  • GridLayout → Components arranged in a grid of rows and columns

  • BoxLayout → Components arranged vertically or horizontally


6. Event Handling

  • Swing uses event-driven programming

  • Events are captured using listeners, e.g., ActionListener, MouseListener, KeyListener

  • Methods like addActionListener() attach event handlers to components


7. Advantages of Swing

  • Platform-independent GUI

  • Rich and flexible components

  • Supports MVC architecture

  • Easy integration with Java 2D API for graphics

  • Pluggable look and feel


8. Key Points

  • Swing is lightweight and pure Java

  • All components inherit from JComponent

  • Use event listeners to handle user actions

  • Combine layout managers for flexible UI design

  • Use JOptionPane for dialogs and messages


9. Conclusion

Java Swing provides a robust, flexible, and platform-independent GUI toolkit. Mastery of Swing enables building interactive desktop applications with buttons, forms, tables, and custom graphics, making Java applications user-friendly and professional.


🔒 Some advanced sections are available for Registered Members
Register Now

Share this Post


← Back to Tutorials

Popular Competitive Exam Quizzes