Swing

 Swing is a part of Java's Java Foundation Classes (JFC), which is used to create graphical user interface (GUI) applications. Swing is a lightweight GUI toolkit, meaning it doesn't rely heavily on the underlying system's GUI libraries. Instead, it uses its own rendering engine, making it platform-independent.

Here’s a step-by-step guide to understanding Swing in Java:


1. Basics of Swing

  • Swing is part of the javax.swing package.
  • It provides a set of "components" like buttons, text fields, labels, etc.
  • Swing follows a Model-View-Controller (MVC) architecture.

2. Setting Up a Swing Application

Every Swing application typically has:

  • A Frame: A main window (like a container).
  • Components: Elements like buttons, labels, and text fields.
  • Layout Manager: To arrange components.

3. Common Swing Components

Here are some commonly used Swing components:

  • JFrame: The main window.
  • JLabel: Displays text or images.
  • JButton: A clickable button.
  • JTextField: A single-line input field.
  • JTextArea: A multi-line text area.
  • JCheckBox: A checkbox for boolean selection.
  • JRadioButton: Radio buttons for mutually exclusive choices.
  • JPanel: A container for grouping components.

4. Swing Layout Managers

Swing uses layout managers to arrange components:

  • FlowLayout: Places components in a row.
  • BorderLayout: Divides the container into five areas (NORTH, SOUTH, EAST, WEST, CENTER).
  • GridLayout: Arranges components in a grid.
  • BoxLayout: Places components in a single row or column.

5. Writing a Simple Swing Application

Here is an example of a basic Swing program:


import javax.swing.*; // Import Swing package public class SwingExample { public static void main(String[] args) { // Create a JFrame (Main Window) JFrame frame = new JFrame("Swing Example"); frame.setSize(400, 300); // Set the size of the window frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Exit the program when closed // Create a JLabel (Text Label) JLabel label = new JLabel("Welcome to Swing!"); // Create a JButton (Button) JButton button = new JButton("Click Me"); // Add components to the frame frame.add(label); // Add the label frame.add(button); // Add the button // Set Layout Manager frame.setLayout(new FlowLayout()); // Flow layout for components // Make the frame visible frame.setVisible(true); } }

6. Adding Event Handling

To make components interactive, we add event listeners. For example:

Adding Action Listener to a Button:


button.addActionListener(e -> { System.out.println("Button was clicked!"); });

7. Key Features of Swing

  • Lightweight: Doesn’t depend heavily on native GUI libraries.
  • Customizable: Supports Look and Feel (e.g., Metal, Nimbus, etc.).
  • Event-Driven: Uses listeners for event handling.
  • Pluggable Look and Feel: Can mimic the native look or have a unique style.

8. Example with Multiple Components


import javax.swing.*; import java.awt.*; public class SwingDemo { public static void main(String[] args) { JFrame frame = new JFrame("Swing Demo"); frame.setSize(500, 400); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Create a panel JPanel panel = new JPanel(); panel.setLayout(new GridLayout(3, 2)); // Grid layout with 3 rows and 2 columns // Add components to the panel panel.add(new JLabel("Name: ")); panel.add(new JTextField(10)); panel.add(new JLabel("Password: ")); panel.add(new JPasswordField(10)); panel.add(new JButton("Submit")); // Add the panel to the frame frame.add(panel); frame.setVisible(true); } }



Comments

Popular posts from this blog

Programming Notes by Atul Kalukhe