Introduction to Swing
Swing is a part of Java Foundation Classes (JFC) used to create Graphical
User Interface (GUI) applications. It provides a rich set of lightweight
components that are platform-independent and more flexible than AWT
(Abstract Window Toolkit). Swing components are derived from the
javax.swing package and follow the Model-View-Controller (MVC)
architecture.
Basic Swing Components
1. Text Fields (JTextField)
Used to accept input from the user in the form of a single line of
text.
Can be set to editable or non-editable.
Common Methods:
o getText(): Retrieves the text.
o setText(String text): Sets the text.
Code Example:
import javax.swing.*;
public class TextFieldExample
public static void main(String[] args)
JFrame frame = new JFrame("Text Field Example");
JTextField textField = new JTextField(20);
textField.setBounds(50, 50, 150, 20);
frame.add(textField);
frame.setSize(300, 200);
frame.setLayout(null);
frame.setVisible(true);
Real-Life Example:
A login form where users enter their usernames
2. Buttons (JButton)
Used to perform an action when clicked.
Can have text or icons as labels.
Common Methods:
o setText(String text): Sets button label.
o addActionListener(ActionListener l): Adds an event listener.
Code Example:
import javax.swing.*;
import java.awt.event.*;
public class ButtonExample {
public static void main(String[] args) {
JFrame frame = new JFrame("Button Example");
JButton button = new JButton("Click Me");
button.setBounds(50, 100, 95, 30);
button.addActionListener(e ->
JOptionPane.showMessageDialog(frame, "Button Clicked!"));
frame.add(button);
frame.setSize(300, 200);
frame.setLayout(null);
frame.setVisible(true);
Real-Life Example:
Submit buttons in forms.
3. Toggle Buttons (JToggleButton)
Can switch between two states: selected or not selected.
Useful for representing on/off or yes/no actions.
import javax.swing.*;
public class ToggleButtonExample {
public static void main(String[] args) {
JFrame frame = new JFrame("Toggle Button Example");
JToggleButton toggleButton = new JToggleButton("OFF");
toggleButton.setBounds(50, 100, 100, 30);
toggleButton.addItemListener(e -> {
toggleButton.setText(toggleButton.isSelected() ? "ON" : "OFF");
});
frame.add(toggleButton);
frame.setSize(300, 200);
frame.setLayout(null);
frame.setVisible(true);
}
}
Real-Life Example:
A Wi-Fi on/off switch in settings.
4. Checkboxes (JCheckBox)
Used to represent a list of options where multiple choices can be
selected.
Common Methods:
o isSelected(): Checks if the checkbox is selected.
import javax.swing.*;
public class CheckBoxExample {
public static void main(String[] args) {
JFrame frame = new JFrame("Checkbox Example");
JCheckBox checkBox1 = new JCheckBox("Option 1");
checkBox1.setBounds(50, 50, 100, 30);
JCheckBox checkBox2 = new JCheckBox("Option 2");
checkBox2.setBounds(50, 100, 100, 30);
frame.add(checkBox1);
frame.add(checkBox2);
frame.setSize(300, 200);
frame.setLayout(null);
frame.setVisible(true);
}
Real-Life Example:
Selecting interests in a survey form
5. Radio Buttons (JRadioButton)
Represent a set of mutually exclusive options (only one can be
selected).
Typically used with a ButtonGroup to enforce exclusivity.
import javax.swing.*;
public class RadioButtonExample {
public static void main(String[] args) {
JFrame frame = new JFrame("Radio Button Example");
JRadioButton r1 = new JRadioButton("Male");
r1.setBounds(50, 50, 100, 30);
JRadioButton r2 = new JRadioButton("Female");
r2.setBounds(50, 100, 100, 30);
ButtonGroup bg = new ButtonGroup();
bg.add(r1);
bg.add(r2);
frame.add(r1);
frame.add(r2);
frame.setSize(300, 200);
frame.setLayout(null);
frame.setVisible(true);
}
Real-Life Example:
Selecting gender in a registration form