In this article Swing in Java we give the information about  java swing is a part of java foundation classes (JFC) which is used to create applications based on Windows.

Swing in Java

Introduction

Swing is part of the Java Foundation Classes (JFC) used to create Graphical User Interface (GUI) applications in Java.
It is built on top of AWT (Abstract Window Toolkit) and is completely written in Java, making it lightweight and platform-independent.

Why Swing?

The AWT library provides basic GUI components but has two main drawbacks:

  1. AWT programs are large in size.
  2. AWT components are fixed and not easily customizable.

Swing was created to overcome these limitations.
Although Swing is based on AWT, it does not replace AWT — it extends it to offer better flexibility and control.

Swing Features

  1. Platform Independent:
    Swing components work the same on all operating systems.
  2. Lightweight:
    Components are written purely in Java, so they do not depend on native OS code.
  3. Rich Controls:
    Swing provides advanced components like JTree, JTabbedPane, JSlider, JColorChooser, and JTable.
  4. Customizable:
    Developers can easily customize look, color, size, and behavior of Swing components.
  5. Pluggable Look and Feel:
    The look and feel of Swing applications can be changed at runtime (e.g., Metal, Nimbus, Windows).

Common Swing Component Classes

The javax.swing package provides many classes, including:

Class Description
JFrame Main window container
JDialog Used to create dialog boxes
JLabel Displays a text label
JButton Creates a clickable button
JTextField Creates a single-line text box
JTextArea Creates a multi-line text area
JCheckBox Creates a check box
JRadioButton Creates a radio button
JList Displays a list of items
JMenuBar Creates a menu bar
JScrollBar Adds a scroll bar
JPanel Lightweight container for grouping components

Important Methods of Swing Components

Method Description
add(Component c) Adds one component to another.
setSize(int width, int height) Sets the size of the component.
setLayout(LayoutManager m) Sets the layout manager.
setVisible(boolean b) Shows or hides the component.

 Example 1: Simple Swing Program

import javax.swing.*;

public class App {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame frame = new JFrame(“Hello World Swing!”);
frame.setSize(500, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}
}

Output:
A window titled “Hello World Swing!” appears on the screen.

Swing Classes Overview

Class Description
JWindow Top-level container without borders or title bar.
JFrame Main application window that can hold components.
JDialog Used for dialog boxes (e.g., message boxes).
JComponent Base class for all Swing components.

 Example 2: JButton Example

import javax.swing.*;

public class SwingDemo {

public static void main(String[] args) {

JFrame frame = new JFrame(“Button Example”);

JButton button = new JButton(“Click Here!”);

button.setBounds(100, 100, 120, 50);

frame.add(button);

frame.setSize(300, 300);

frame.setLayout(null);

frame.setVisible(true);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

}

Output:
A window opens with a button labeled “Click Here!”.

Swing Containers

Swing uses containers to hold and organize components.

There are two types of containers:

  1. Top-Level Containers
    • Examples: JFrame, JApplet, JDialog, JWindow
    • They inherit AWT container classes and are heavyweight.
  2. Lightweight Containers
    • Example: JPanel
    • Used to organize components within top-level containers.
    • Inherits JComponent.

Common Swing Components

  1. JButton

Used to create a clickable button.

Declaration:

public class JButton extends AbstractButton implements Accessible

Constructors:

JButton()                      // No text or icon

JButton(String text)           // With text

JButton(Icon icon)             // With icon

JButton(String text, Icon icon)// With both

  1. JTextField

Used for single-line text input.

Declaration:

public class JTextField extends JTextComponent implements SwingConstants

Constructors:

JTextField()

JTextField(String text)

  1. JCheckBox

Used to create a checkbox.

Declaration:

public class JCheckBox extends JToggleButton implements Accessible

Constructors:

JCheckBox()

JCheckBox(String text)

JCheckBox(Icon icon)

JCheckBox(String text, Icon icon)

Example 3: JFrame with Multiple Components

import javax.swing.*;

public class MultiComponentDemo {

public static void main(String[] args) {

JFrame frame = new JFrame(“Swing Example”);

JLabel label = new JLabel(“Enter Name:”);

label.setBounds(50, 50, 100, 30);

JTextField textField = new JTextField();

textField.setBounds(150, 50, 150, 30);

JCheckBox checkBox = new JCheckBox(“I agree”);

checkBox.setBounds(50, 100, 100, 30);

JButton button = new JButton(“Submit”);

button.setBounds(150, 150, 100, 30);

frame.add(label);

frame.add(textField);

frame.add(checkBox);

frame.add(button);

frame.setSize(400, 300);

frame.setLayout(null);

frame.setVisible(true);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

}

Output:
A window with a label, text field, checkbox, and button.

Some More: 

POP- Introduction to Programming Using ‘C’

DS – Data structure Using C

OOP – Object Oriented Programming 

Java Programming

DBMS – Database Management System

RDBMS – Relational Database Management System

Join Now: Data Warehousing and Data Mining 

Leave a Reply

Your email address will not be published. Required fields are marked *