Question: Summarize any three features of Swing API

Answer:

Swing is a set of GUI (Graphical User Interface) components for Java programs. It provides a rich toolkit for creating user interfaces in Java applications. Here are three notable features of the Swing API:

1. Lightweight Components:

Swing components are lightweight, meaning they are implemented entirely in Java and do not rely on platform-specific implementations. This makes Swing applications highly portable and ensures consistent behavior across different operating systems.

Example:

import javax.swing.*;

public class HelloWorldSwing {
    public static void createAndShowGUI() {
        // Create a JFrame
        JFrame frame = new JFrame("HelloWorldSwing");
        // Add a JLabel to the frame
        JLabel label = new JLabel("Hello, World!");
        frame.getContentPane().add(label);
        // Set frame properties
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        // Schedule a job for the event-dispatching thread:
        // creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(() -> createAndShowGUI());
    }
}

2. Pluggable Look and Feel:

Swing provides support for pluggable look and feel, allowing developers to customize the appearance of their applications. It offers different look and feel options, such as the default Metal look and feel, the Windows look and feel, and the Motif look and feel, among others.

Example:

import javax.swing.*;

public class LookAndFeelExample {
    public static void main(String[] args) {
        // Set the look and feel to the Windows look and feel
        try {
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
        } catch (Exception e) {
            e.printStackTrace();
        }

        // Create and show a JFrame with customized look and feel
        JFrame frame = new JFrame("LookAndFeelExample");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JButton button = new JButton("Click Me");
        frame.getContentPane().add(button);
        frame.pack();
        frame.setVisible(true);
    }
}

3. Layout Managers:

Swing provides layout managers to help arrange components within containers in a flexible and dynamic manner. Layout managers handle component positioning and sizing, allowing GUIs to adapt to different screen sizes and resolutions.

Example:

import javax.swing.*;
import java.awt.*;

public class LayoutManagerExample {
    public static void main(String[] args) {
        // Create a JFrame
        JFrame frame = new JFrame("LayoutManagerExample");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // Create components
        JButton button1 = new JButton("Button 1");
        JButton button2 = new JButton("Button 2");
        JButton button3 = new JButton("Button 3");

        // Create a JPanel with GridLayout
        JPanel panel = new JPanel(new GridLayout(3, 1));
        panel.add(button1);
        panel.add(button2);
        panel.add(button3);

        // Add panel to the frame
        frame.getContentPane().add(panel);

        // Pack and display the frame
        frame.pack();
        frame.setVisible(true);
    }
}

Conclusion:

The Swing API in Java provides a powerful toolkit for building graphical user interfaces in Java applications. With features such as lightweight components, pluggable look and feel, and layout managers, Swing enables developers to create rich and interactive GUIs that are portable and customizable.

Leave a Comment