Introduction to Java Swing

Introduction to Java Swing

Java Swing is a part of Java Foundation Classes (JFC) and provides a set of APIs for creating graphical user interfaces (GUIs) in Java. It is built on top of the Abstract Window Toolkit (AWT) and offers a richer set of GUI components. Swing is a platform-independent, lightweight toolkit that can be used to create window-based applications.

Key Features of Swing

  1. Lightweight Components:

    • Swing components are lightweight because they are written entirely in Java and do not rely on native platform components.
  2. Pluggable Look and Feel:

    • Swing supports pluggable look and feel, which means you can change the appearance of your application to match different platforms (e.g., Windows, macOS, Linux) or create a custom look and feel.
  3. Component-based Architecture:

    • Swing follows a component-based architecture, allowing for reusable, modular, and easily extendable components.
  4. MVC Architecture:

    • Swing components follow the Model-View-Controller (MVC) design pattern, separating the data (Model), the UI (View), and the interaction logic (Controller).
  5. Event-Driven Programming:

    • Swing supports event-driven programming, where user actions (e.g., button clicks) trigger events that can be handled by event listeners.
  6. Customizable Components:

    • Swing components can be customized by extending existing components or creating new ones from scratch.
Basic Components and Containers

Swing provides a rich set of components and containers that can be used to build GUIs. Here are some of the most commonly used ones:

  1. JFrame:

    • The main window that contains other components.
    • Example: JFrame frame = new JFrame("My Application");
  2. JPanel:

    • A generic container used to group other components.
    • Example: JPanel panel = new JPanel();
  3. JButton:

    • A button that can trigger actions when clicked.
    • Example: JButton button = new JButton("Click Me");
  4. JLabel:

    • A text label for displaying static text or images.
    • Example: JLabel label = new JLabel("Hello, World!");
  5. JTextField:

    • A single-line text input field.
    • Example: JTextField textField = new JTextField(20);
  6. JTextArea:

    • A multi-line text input area.
    • Example: JTextArea textArea = new JTextArea(5, 20);
  7. JComboBox:

    • A drop-down list for selecting an item from a list of options.
    • Example: JComboBox<String> comboBox = new JComboBox<>(new String[] {"Option 1", "Option 2"});
  8. JCheckBox:

    • A check box for selecting/deselecting options.
    • Example: JCheckBox checkBox = new JCheckBox("Check me");
Example: A Simple Swing Application

Here's a basic example of a Swing application that demonstrates how to create a window with a button and a label:

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class SimpleSwingApp {
    public static void main(String[] args) {
        // Create the main frame
        JFrame frame = new JFrame("Simple Swing Application");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(400, 300);

        // Create a panel to hold components
        JPanel panel = new JPanel();
        frame.add(panel);
        placeComponents(panel);

        // Make the frame visible
        frame.setVisible(true);
    }

    private static void placeComponents(JPanel panel) {
        panel.setLayout(null);

        // Create a label
        JLabel label = new JLabel("Hello, Swing!");
        label.setBounds(150, 20, 100, 25);
        panel.add(label);

        // Create a button
        JButton button = new JButton("Click Me");
        button.setBounds(150, 60, 100, 25);
        panel.add(button);

        // Add action listener to the button
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                label.setText("Button Clicked!");
            }
        });
    }
}

Explanation:

  1. Creating the Frame:

    • JFrame frame = new JFrame("Simple Swing Application"); creates the main window.
    • frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); ensures the application exits when the window is closed.
    • frame.setSize(400, 300); sets the window size.
  2. Creating the Panel:

    • JPanel panel = new JPanel(); creates a panel to hold other components.
    • frame.add(panel); adds the panel to the frame.
    • panel.setLayout(null); sets the layout manager to null for absolute positioning.
  3. Adding Components:

    • JLabel label = new JLabel("Hello, Swing!"); creates a label.
    • JButton button = new JButton("Click Me"); creates a button.
    • Both components are added to the panel with specified bounds for positioning.
  4. Handling Events:

    • button.addActionListener(new ActionListener() { ... }); adds an action listener to handle button clicks.
    • label.setText("Button Clicked!"); updates the label text when the button is clicked.

This example demonstrates the basics of creating a simple GUI application using Java Swing. Swing provides a powerful and flexible framework for building rich user interfaces in Java applications.

Comments

Popular posts from this blog

KTU OOP LAB JAVA CSL 203 BTech CS S3 - Dr Binu V P

Syllabus and Practice Questions KTU OOPS Lab Java CSL 203

String Problems