Unit V Swing
Unit V Swing
Introduction to Swing (as per Herbert Schildt’s The Java Complete Reference, 7th Ed.)
Swing is a part of Java’s Standard Library that provides a rich set of GUI (Graphical
User Interface) components for building desktop applications. It is included in the Java
Foundation Classes (JFC) and offers more sophisticated components than the older AWT
(Abstract Window Toolkit).
Swing was introduced with Java 2 (JDK 1.2) and allows developers to build platform-
independent, visually rich, and user-interactive applications.
Containers in Swing
In Swing, a container is a component that can hold and organize other GUI components
like buttons, labels, and text fields. All containers inherit from the class java.awt.Container,
which itself is a subclass of java.awt.Component.
Herbert Schildt explains containers as the foundation for laying out other components in a
GUI application.
1. Top-Level Containers
These are the starting points of any Swing application. You cannot add components directly
to them—you must add components to their content panes.
Container Description
JFrame A main application window. Most commonly used.
JDialog A pop-up dialog window. Can be modal or modeless.
JApplet A container for applets (now outdated).
➤ Example:
JFrame frame = new JFrame("My Window");
frame.setSize(400, 300);
frame.setVisible(true);
2. Lightweight Containers
These are used inside top-level containers to organize components using layout managers.
Container Description
JPanel Generic lightweight container for grouping components.
JScrollPane Provides scrollbars to view large components.
JTabbedPane Holds multiple panels with tabs.
JSplitPane Splits space between two components.
JLayeredPane Allows components to overlap each other.
➤ Example using JPanel:
JPanel panel = new JPanel();
panel.add(new JButton("Click Me"));
Top-Level Containers
In Java Swing, a top-level container is the foundation on which all Swing applications are
built. These containers are the entry point of the GUI, and they are responsible for holding
all other components and containers (like buttons, text fields, panels, etc.).
Herbert Schildt explains that without a top-level container, you cannot create a visible
GUI window in Swing.
📘 JPanel in Swing
In Java Swing, JPanel is a generic lightweight container used to organize and group other
components in a GUI. According to Herbert Schildt, JPanel is one of the most commonly used
containers and plays a vital role in managing layout and component arrangement.
// JToggleButton component
JToggleButton toggleButton = new JToggleButton("OFF");
🧠 Schildt’s Insights
Herbert Schildt emphasizes:
Use JToggleButton when state matters (e.g., ON/OFF, BOLD/UNBOLD).
Combine with ButtonGroup to create mutually exclusive toggles (like radio buttons).
It is part of the Swing component hierarchy, and should always be used inside a top-
level container for it to be visible and interactive.
JCheckBox
🔹 What is JCheckBox?
JCheckBox is a Swing component that represents a check box — a small box that can be
either checked (true) or unchecked (false).
It is part of the javax.swing package and is a subclass of AbstractButton, which means it
behaves like a button but retains a selected state.
↳ java.awt.Component
java.lang.Object
↳ java.awt.Container
↳ javax.swing.JComponent
↳ javax.swing.AbstractButton
↳ javax.swing.JToggleButton
↳ javax.swing.JCheckBox
So, a JCheckBox is a specialized type of toggle button that supports two states: checked and
unchecked.
🔑 Constructors
Herbert Schildt highlights the following commonly used constructors:
JCheckBox() // Creates an unchecked check box with no text
JCheckBox(String text) // Creates an unchecked check box with a label
JCheckBox(String text, boolean selected) // Creates with a label and initial state
submit.addActionListener(e -> {
if (checkBox.isSelected()) {
JOptionPane.showMessageDialog(frame, "Thank you for agreeing!");
} else {
JOptionPane.showMessageDialog(frame, "Please agree to continue.");
}
});
frame.add(checkBox);
frame.add(submit);
frame.setVisible(true);
}
}
How It Works
A JCheckBox is added to a JFrame (a top-level container).
When the "Submit" button is clicked, it checks whether the box is selected using
isSelected().
Based on the state, a message is displayed.
2️.JLabel
JLabel is a non-editable text component used to display static text or an image.
JLabel label = new JLabel("Username:");
✅ Use Case: Descriptive labels for form fields.
3️⃣ JTextField
A JTextField is a single-line input field that allows users to enter and edit text.
JTextField textField = new JTextField(20);
✅ Use Case: Name, email, username input, etc.
4️JTextArea
JTextArea is a multi-line area for displaying or editing plain text.
JTextArea textArea = new JTextArea(5, 20);
✅ Use Case: Comments, address fields, logs, etc.
5️JList
JList is used to display a list of items, allowing single or multiple selection.
String[] colors = {"Red", "Green", "Blue"};
JList<String> colorList = new JList<>(colors);
✅ Use Case: Selection of multiple options, like hobbies, skills, etc.
6️JComboBox
A JComboBox is a drop-down list that lets users choose one item from a list.
String[] countries = {"India", "USA", "UK"};
JComboBox<String> comboBox = new JComboBox<>(countries);
✅ Use Case: Country selection, category selection, etc.
7️JScrollPane
A JScrollPane provides a scrolling view of another component, like a JTextArea, JList, or
even a JPanel.
JTextArea area = new JTextArea(10, 30);
JScrollPane scrollPane = new JScrollPane(area);
✅ Use Case: When the content exceeds the visible area.
// JRadioButton
JRadioButton male = new JRadioButton("Male");
JRadioButton female = new JRadioButton("Female");
male.setBounds(120, 60, 70, 25);
female.setBounds(200, 60, 80, 25);
ButtonGroup genderGroup = new ButtonGroup();
genderGroup.add(male);
genderGroup.add(female);
// JList
JLabel listLabel = new JLabel("Select Color:");
listLabel.setBounds(20, 220, 100, 25);
String[] colors = {"Red", "Green", "Blue"};
JList<String> colorList = new JList<>(colors);
JScrollPane listScroll = new JScrollPane(colorList);
listScroll.setBounds(120, 220, 100, 60);
// JComboBox
JLabel countryLabel = new JLabel("Country:");
countryLabel.setBounds(20, 300, 100, 25);
JComboBox<String> countryBox = new JComboBox<>(new String[]{"India", "USA", "UK"});
countryBox.setBounds(120, 300, 120, 25);
frame.add(nameLabel);
frame.add(nameField);
frame.add(male);
frame.add(female);
frame.add(commentLabel);
frame.add(scrollPane);
frame.add(listLabel);
frame.add(listScroll);
frame.add(countryLabel);
frame.add(countryBox);
frame.setVisible(true);
}
}
🧠 Summary Table
Component Purpose
JRadioButton Single-choice option in a group
JLabel Display non-editable text
JTextField Single-line text input
JTextArea Multi-line text input
JList List of selectable items
JComboBox Drop-down for single selection
JScrollPane Adds scrollbars to other components