Java Swing Cheat
Sheet
1. Main Window
a. JFrame
b. JPanel
2. Core Components
a. Components Management
b. JLabel
c. JButton
d. JTextField
e. JTextArea
f. TCheckBox
g. JRadioButton
3. Event Handling
a. ActionListener
b. ItemListener
c. KeyListener
d. MouseListener
4. Layouts
a. FlowLayout
5. Full Example
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
Main Window
JFrame
The main window.
public class Main{
public static void main(String[] args){
JFrame frame = new JFrame();
//Write here
}
}
Properties
Property Method Description
Title frame.setTitle("Ex") Sets the window title.
Size frame.setSize(400,400) Sets the width and height of the frame.
(height, width)
Visibility frame.setVisible(true) Shows or hides the window.
Background frame.getContentPane().setBa
Color ckground(Color.RED);
JPanel
Container for grouping components, it as a subsection of a window (JFrame) that
can hold buttons, labels, text fields, and more.
JPanel panel = new JPanel();
panel.setBackground(Color.LIGHT_GRAY);
panel.setLayout(new FlowLayout());
JButton button = new JButton("Click me");
panel.add(button);
frame.add(panel); // Add the panel to the JFrame
Same Properties as JFrame.
Core Components
Component Management
Property Method Description
Add Component add(Component comp) Adds a component to
the frame.
Remove Component remove(Component comp) Removes a specific
component.
Clear All Components getContentPane().remov Removes all components
eAll() from the frame.
Validate Layout validate() Re-lays out components
after changes.
Repaint repaint() Refreshes the display.
JLabel
Displays text or an image (non-interactive).
JLabel label = new JLabel("Welcome!");
Useful Methods:
● setText("New text") – changes the displayed text
● setIcon(new ImageIcon("img.png")) – sets an image
JButton
A clickable button that performs an action.
JButton button = new JButton("Submit");
● setText("OK") – changes button text
● addActionListener(e -> { ... }) – responds to clicks
JTextField
Single-line input text
JTextField textField = new JTextField(20);
● getText() – retrieves input
● setText("Default") – sets default text
● addActionListener(e -> { ... }) – listens to Enter key press
JTextArea
Multi-line text box
JTextArea textArea = new JTextArea(5, 20);
● getText() – retrieves text
● setText("Hello") – sets text
JCheckBox
Toggle option (on/off).
JCheckBox checkBox = new JCheckBox("I agree");
● isSelected() – returns true if checked
● addItemListener(e -> { ... }) – handles check/uncheck
events
JRadioButton
Select one option in a group.
JRadioButton rb = new JRadioButton("Male");
ButtonGroup group = new ButtonGroup();
group.add(rb);
● isSelected() – checks if this button is selected
● addItemListener(e -> { ... }) – handles selection
Use ButtonGroup to group JRadioButtons so only one can be selected at
a time.
Event Handling
In Swing, event handling lets components respond to user actions like clicks,
key presses, or window events. This is done by adding listeners to
components.
ActionListener– Button Clicks / Enter Key
JButton button = new JButton("Click me");
button.addActionListener(e -> {
System.out.println("Button clicked!");
});
ItemListener-Checkbox & RadioButton Selection
JCheckBox checkBox = new JCheckBox("Agree?");
checkBox.addItemListener(e -> {
if (checkBox.isSelected()) {
System.out.println("Checked");
} else {
System.out.println("Unchecked");
}
});
KeyListener-keyboard Input
textField.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
System.out.println("Key pressed: " + e.getKeyChar());
}
});
MouseListener-Mouse Clicks
component.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
System.out.println("Mouse clicked!");
}
});
Layouts
FlowLayout
Places components left to right, like text in a paragraph.
frame.setLayout(new FlowLayout());
● new FlowLayout(FlowLayout.LEFT) – aligns to the left
● new FlowLayout(FlowLayout.CENTER) – (default)
● new FlowLayout(FlowLayout.RIGHT) – aligns to the right
Full Example
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Main {
🪟
public static void main(String[] args) {
// Main Window
JFrame frame = new JFrame("Cheat Sheet App with Image");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600, 600);
// 🧱 Panel with Layout
JPanel panel = new JPanel(new FlowLayout());
// 🏷️ JLabel
JLabel label = new JLabel("Enter your name:");
// 🖊️ JTextField
JTextField textField = new JTextField(20);
// 📝 JTextArea
JTextArea textArea = new JTextArea(5, 30);
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
//✅ JCheckBox
JCheckBox checkBox = new JCheckBox("I agree to the
terms");
// 🔘 JRadioButtons and Group
JRadioButton male = new JRadioButton("Male");
JRadioButton female = new JRadioButton("Female");
ButtonGroup genderGroup = new ButtonGroup();
genderGroup.add(male);
genderGroup.add(female);
// 🖱️ JButton
JButton button = new JButton("Submit");
// 🖼️ JLabel for Image
JLabel imageLabel = new JLabel();
ImageIcon imageIcon = new ImageIcon("My Image.jpg");
imageLabel.setIcon(imageIcon);
//🎯 ActionListener for button
button.addActionListener(e -> {
String name = textField.getText();
String gender = male.isSelected() ? "Male" :
(female.isSelected() ? "Female" : "Unspecified");
String agreement = checkBox.isSelected() ? "Agreed" :
"Not agreed";
textArea.setText("Name: " + name + "\nGender: " +
gender + "\nStatus: " + agreement);
});
// 🎯 ItemListener for checkbox
checkBox.addItemListener(e -> {
if (checkBox.isSelected()) {
System.out.println("Checkbox checked");
} else {
System.out.println("Checkbox unchecked");
}
});
// 🎯 KeyListener for Enter key in text field
textField.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
button.doClick(); // simulate button click
}
}
});
//🎯 MouseListener for text area
textArea.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
System.out.println("TextArea clicked!");
}
});
//➕ Add Components to Panel
panel.add(label);
panel.add(textField);
panel.add(male);
panel.add(female);
panel.add(checkBox);
panel.add(button);
🖼️
panel.add(textArea);
panel.add(imageLabel); // Add image at the end
//🧱 Add Panel to Frame
frame.add(panel);
//👀 Show Frame
frame.setVisible(true);
}
}