Data Types and Operators
(Non-Primitive Data Types)
Control Flow Statements
Conditional Statements
Looping Statements
Branching Statements
Object-Oriented Programming (OOP)
Exception Handling
Collections Framework
Overview of Collections
Java I/O
Multithreading
GUI Programming with Swing
Advanced Topics
JAVA CODE
Java Basics
Working with Objects
Arrays, Conditionals, and Loops
Creating Classes and Applications in Java
More About Methods
Java Applet Basics
Graphics, Fonts, and Color
Simple Animation and Threads
More Animation, Images, and Sound
Managing Simple Events and Interactivity
Creating User Interfaces with the awt
Windows, Networking, and Other Tidbits
Modifiers, Access Control, and Class Design
Packages and Interfaces
Exceptions
Multithreading
Streams and I/O
Using Native Methods and Libraries
Under the Hood
Java Programming Tools
Working with Data Structures in Java
Advanced Animation and Media
Fun with Image Filters
Client/Server Networking in Java
Emerging Technologies
appendix A :- Language Summary
appendix B :- Class Hierarchy Diagrams
appendix C The Java Class Library
appendix D Bytecodes Reference
appendix E java.applet Package Reference
appendix F java.awt Package Reference
appendix G java.awt.image Package Reference
appendix H java.awt.peer Package Reference
appendix I java.io Package Reference
appendix J java.lang Package Reference
appendix K java.net Package Reference
appendix L java.util Package Reference

Swing is a part of Java Foundation Classes (JFC) and is used to create window-based applications. Swing provides a rich set of components for building graphical user interfaces (GUIs) and adds interactivity to Java applications. Unlike AWT (Abstract Window Toolkit), Swing components are lightweight and platform-independent, meaning they are written entirely in Java and do not rely on native GUI components.

Basics of Swing Components

  1. JFrame: This is the main window container. It’s where other Swing components are added.
  2. JPanel: A generic container that can store a group of components. It is often used to organize the layout.
  3. JButton: A button component that triggers an action when clicked.
  4. JLabel: A display area for a short text string or an image.
  5. JTextField: A single-line text field that allows the user to input text.
  6. JTextArea: A multi-line text area to display/edit text.
  7. JCheckBox: A box that can be checked or unchecked.
  8. JRadioButton: A radio button that can be selected or deselected.
  9. JComboBox: A drop-down list that allows the user to choose from a set of options.
  10. JList: A component that displays a list of items for selection.

Example: Simple Swing Application

Let’s create a simple Swing application that includes a few basic components: a ‘JFrame‘, ‘JPanel‘, ‘JButton‘, ‘JLabel‘, and ‘JTextField‘.

Step 1: Import Required Packages

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

				
			

Step 2: Create the Main Class

				
					public class SwingExample {
    public static void main(String[] args) {
        // Create a new JFrame container
        JFrame frame = new JFrame("Swing Example");

        // Set the initial size of the frame
        frame.setSize(400, 200);

        // Specify an action for the close button
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // Create a JPanel to hold components
        JPanel panel = new JPanel();
        panel.setLayout(new FlowLayout());

        // Create a JLabel
        JLabel label = new JLabel("Enter your name:");

        // Create a JTextField
        JTextField textField = new JTextField(20);

        // Create a JButton
        JButton button = new JButton("Submit");

        // Add ActionListener to the button
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // Get the text from the text field
                String name = textField.getText();
                // Display a message dialog with the entered name
                JOptionPane.showMessageDialog(frame, "Hello, " + name + "!");
            }
        });

        // Add components to the panel
        panel.add(label);
        panel.add(textField);
        panel.add(button);

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

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

				
			

Explanation

  • JFrame: This is the main window of our application. We create a JFrame object and set its size and default close operation. The setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) method ensures that the application exits when the frame is closed.

  • JPanel: This is a container that holds and organizes other components. We set its layout to FlowLayout, which arranges components in a left-to-right flow, much like lines of text in a paragraph.

  • JLabel: A simple text label that prompts the user to enter their name.

  • JTextField: A single-line text input field where the user can enter their name. We set its width to 20 columns.

  • JButton: A button labeled “Submit”. When clicked, it triggers an action defined in its ActionListener.

  • ActionListener: An interface used to receive action events. We implement it using an anonymous class. When the button is clicked, the actionPerformed() method is called, which retrieves the text from the JTextField and displays it in a message dialog using JOptionPane.

  • Adding Components: We add the JLabel, JTextField, and JButton to the JPanel, and then add the JPanel to the JFrame.

  • Visibility: Finally, we set the frame’s visibility to true, which makes the window appear on the screen.

Additional Components

JCheckBox and JRadioButton

				
					// Create JCheckBox
JCheckBox checkBox = new JCheckBox("I agree");

// Create JRadioButton
JRadioButton radioButton1 = new JRadioButton("Option 1");
JRadioButton radioButton2 = new JRadioButton("Option 2");

// Group radio buttons
ButtonGroup group = new ButtonGroup();
group.add(radioButton1);
group.add(radioButton2);

// Add to panel
panel.add(checkBox);
panel.add(radioButton1);
panel.add(radioButton2);

				
			

JComboBox and JList

				
					// Create JComboBox
String[] choices = {"Choice 1", "Choice 2", "Choice 3"};
JComboBox<String> comboBox = new JComboBox<>(choices);

// Create JList
JList<String> list = new JList<>(choices);
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

// Add to panel
panel.add(comboBox);
panel.add(new JScrollPane(list));

				
			

Swing provides a comprehensive set of components for building rich and interactive GUIs. By combining these components and managing their events, you can create sophisticated desktop applications. The example above demonstrates how to get started with some of the basic components. From here, you can explore more advanced features and components in the Swing library to enhance your applications.

Scroll to Top