COMPONENTS
🎨 Java Swing Masterclass Reference – Exam Ready
1️⃣ JFrame – The Main Window
Feature Description
Class JFrame extends Frame (AWT).
Usage Main window for Swing applications.
Create JFrame JFrame frame = new JFrame("Title");
Set size [Link](width, height);
Close operation [Link](JFrame.EXIT_ON_CLOSE);
Visibility [Link](true);
Layouts Default: BorderLayout . Use setLayout() to change.
ContentPane Container inside JFrame where components go. Use getContentPane() to access.
2️⃣ JPanel – A Simple Container
Feature Description
Class JPanel is a container for components.
Usage Groups multiple components.
Add to JFrame [Link](panel);
Layouts Default: FlowLayout .
3️⃣ JLabel – Display Text or Image
Feature Description
Class JLabel
Create JLabel label = new JLabel("Text");
Set text [Link]("New Text");
Set image [Link](new ImageIcon("[Link]"));
Non-interactive Cannot be clicked.
4️⃣ JButton – A Clickable Button
Feature Description
Class JButton
Create JButton btn = new JButton("Click Me");
Add action [Link](e -> {});
Get text [Link]();
5️⃣ JTextField – Single-Line Text Input
Feature Description
Class JTextField
Create JTextField tf = new JTextField(20);
Get text [Link]();
Set text [Link]("Hello");
COMPONENTS 1
6️⃣ JPasswordField – Hidden Input (Password)
Feature Description
Class JPasswordField
Create JPasswordField pf = new JPasswordField(20);
Get password [Link](); (returns char[] )
7️⃣ JTextArea – Multi-Line Text Input
Feature Description
Class JTextArea
Create JTextArea ta = new JTextArea(5, 20);
Get text [Link]();
Set text [Link]("Hello");
Scrollbars Wrap in JScrollPane .
8️⃣ JComboBox – Drop-Down Selection
Feature Description
Class JComboBox
Create JComboBox<String> cb = new JComboBox<>(items);
Get selection [Link]();
Add item [Link]("New Item");
Set editable [Link](true);
9️⃣ JList – Vertical List of Items
Feature Description
Class JList
Create JList<String> list = new JList<>(items);
Selection modes SINGLE_SELECTION , MULTIPLE_INTERVAL_SELECTION
Get selection [Link](); or [Link]();
Scroll Always wrap in JScrollPane .
🔟 JCheckBox – Multiple Toggle Options
Feature Description
Class JCheckBox
Create JCheckBox cb = new JCheckBox("Option");
Get state [Link]();
Set state [Link](true/false);
1️⃣1️⃣ JRadioButton – Single Selection Option
Feature Description
Class JRadioButton
Create JRadioButton rb = new JRadioButton("Option");
Grouping Use ButtonGroup to group.
Get state [Link]();
🔑
COMPONENTS 2
🔑 Common Patterns
Action Code Example
Adding to JFrame [Link](component);
Adding to JPanel [Link](component);
Handling Events (e.g., clicks) [Link](e -> { });
Creating layout [Link](new FlowLayout());
Scrollable components new JScrollPane(component)
Set visibility [Link](true);
Get component data .getText() , .getSelectedItem() , .isSelected()
COMPONENTS 3