Java Swing Event Handling - Step-by-Step Programs
Step 1: Basic Frame with Button
import [Link].*;
public class Step1_BasicFrame {
public static void main(String[] args) {
JFrame frame = new JFrame("Step 1: Basic Frame");
JButton button = new JButton("Click Me");
[Link](100, 100, 120, 40);
[Link](button);
[Link](300, 300);
[Link](null);
[Link](true);
[Link](JFrame.EXIT_ON_CLOSE);
}
}
Step 2: Button Click Event Handling
import [Link].*;
import [Link].*;
public class Step2_ButtonClickEvent {
public static void main(String[] args) {
JFrame frame = new JFrame("Step 2: Button Event");
JButton button = new JButton("Click Me");
[Link](100, 100, 120, 40);
[Link](new ActionListener() {
public void actionPerformed(ActionEvent e) {
[Link](frame, "Button Clicked!");
}
});
[Link](button);
[Link](300, 300);
[Link](null);
[Link](true);
[Link](JFrame.EXIT_ON_CLOSE);
}
}
Step 3: Text Input Event Handling
import [Link].*;
import [Link].*;
public class Step3_TextFieldEvent {
public static void main(String[] args) {
JFrame frame = new JFrame("Step 3: Text Input");
JTextField textField = new JTextField();
JButton button = new JButton("Submit");
[Link](50, 50, 200, 30);
[Link](100, 100, 100, 30);
[Link](e -> {
String name = [Link]();
[Link](frame, "Hello, " + name);
});
[Link](textField);
[Link](button);
[Link](300, 250);
[Link](null);
[Link](true);
[Link](JFrame.EXIT_ON_CLOSE);
}
}
Step 4: CheckBox and RadioButton
import [Link].*;
import [Link].*;
public class Step4_CheckBoxRadioButton {
public static void main(String[] args) {
JFrame frame = new JFrame("Step 4: CheckBox & Radio");
JCheckBox cbJava = new JCheckBox("Java");
JCheckBox cbPython = new JCheckBox("Python");
JRadioButton rMale = new JRadioButton("Male");
JRadioButton rFemale = new JRadioButton("Female");
ButtonGroup genderGroup = new ButtonGroup();
[Link](rMale);
[Link](rFemale);
JButton button = new JButton("Submit");
[Link](50, 30, 100, 30);
[Link](150, 30, 100, 30);
[Link](50, 70, 100, 30);
[Link](150, 70, 100, 30);
[Link](100, 120, 100, 30);
[Link](e -> {
String skills = "Skills: ";
if ([Link]()) skills += "Java ";
if ([Link]()) skills += "Python ";
String gender = [Link]() ? "Male" : ([Link]() ?
"Female" : "Not selected");
[Link](frame, skills + "\nGender: " + gender);
});
[Link](cbJava); [Link](cbPython);
[Link](rMale); [Link](rFemale);
[Link](button);
[Link](350, 230);
[Link](null);
[Link](true);
[Link](JFrame.EXIT_ON_CLOSE);
}
}
Step 5: ComboBox and JList
import [Link].*;
import [Link].*;
public class Step5_ComboBoxList {
public static void main(String[] args) {
JFrame frame = new JFrame("Step 5: ComboBox & List");
String[] countries = {"India", "USA", "UK", "Germany"};
String[] colors = {"Red", "Green", "Blue", "Yellow"};
JComboBox<String> countryBox = new JComboBox<>(countries);
JList<String> colorList = new JList<>(colors);
JButton button = new JButton("Show Selection");
[Link](50, 30, 150, 30);
[Link](50, 70, 100, 80);
[Link](50, 170, 150, 30);
[Link](e -> {
String country = (String) [Link]();
String color = [Link]();
[Link](frame, "Country: " + country + "\nColor: " +
color);
});
[Link](countryBox);
[Link](colorList);
[Link](button);
[Link](300, 300);
[Link](null);
[Link](true);
[Link](JFrame.EXIT_ON_CLOSE);
}
}
Step 6: Mouse Events
import [Link].*;
import [Link].*;
public class Step6_MouseEvents {
public static void main(String[] args) {
JFrame frame = new JFrame("Step 6: Mouse Events");
JLabel label = new JLabel("Click anywhere!");
[Link](80, 50, 200, 30);
[Link](new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
[Link]("Clicked at: " + [Link]() + ", " + [Link]());
}
});
[Link](label);
[Link](300, 200);
[Link](null);
[Link](true);
[Link](JFrame.EXIT_ON_CLOSE);
}
}
Step 7: Window Events
import [Link].*;
import [Link].*;
public class Step7_WindowEvents {
public static void main(String[] args) {
JFrame frame = new JFrame("Step 7: Window Events");
JLabel label = new JLabel("Try minimizing or closing this window.");
[Link](30, 50, 300, 30);
[Link](new WindowAdapter() {
public void windowIconified(WindowEvent e) {
[Link]("Window Minimized");
}
public void windowDeiconified(WindowEvent e) {
[Link]("Window Restored");
}
public void windowClosing(WindowEvent e) {
[Link]("Window is closing...");
[Link](frame, "Closing App...");
[Link](0);
}
});
[Link](label);
[Link](350, 200);
[Link](null);
[Link](true);
}
}
Mini-Project: Swing-Based Student Feedback Form
Description:
This project demonstrates a Swing-based GUI for student feedback collection. It integrates JTextField,
JRadioButton, JComboBox, JList, JCheckBox, and event handling for buttons to dynamically display the
user's input.
Complete Code:
import [Link].*;
import [Link].*;
public class StudentFeedbackForm {
public static void main(String[] args) {
JFrame frame = new JFrame("Student Feedback Form");
JLabel nameLabel = new JLabel("Name:");
JTextField nameField = new JTextField();
JLabel genderLabel = new JLabel("Gender:");
JRadioButton male = new JRadioButton("Male");
JRadioButton female = new JRadioButton("Female");
ButtonGroup genderGroup = new ButtonGroup();
JLabel courseLabel = new JLabel("Course:");
String[] courses = {"Java", "Python", "C++"};
JComboBox<String> courseBox = new JComboBox<>(courses);
JLabel ratingLabel = new JLabel("Rate the course:");
JList<String> ratingList = new JList<>(new String[]{"Excellent", "Good", "Average",
"Poor"});
JCheckBox practical = new JCheckBox("Practical");
JCheckBox theory = new JCheckBox("Theory");
JButton submitButton = new JButton("Submit");
JLabel outputLabel = new JLabel("");
// Setting Bounds
[Link](30, 20, 100, 25);
[Link](140, 20, 150, 25);
[Link](30, 60, 100, 25);
[Link](140, 60, 70, 25);
[Link](210, 60, 80, 25);
[Link](30, 100, 100, 25);
[Link](140, 100, 150, 25);
[Link](30, 140, 120, 25);
[Link](140, 140, 150, 60);
[Link](30, 210, 100, 25);
[Link](140, 210, 100, 25);
[Link](100, 250, 120, 30);
[Link](30, 290, 300, 40);
// Adding components
[Link](male);
[Link](female);
[Link](nameLabel);
[Link](nameField);
[Link](genderLabel);
[Link](male);
[Link](female);
[Link](courseLabel);
[Link](courseBox);
[Link](ratingLabel);
[Link](ratingList);
[Link](practical);
[Link](theory);
[Link](submitButton);
[Link](outputLabel);
// Action Listener
[Link](e -> {
String name = [Link]();
String gender = [Link]() ? "Male" : ([Link]() ? "Female" :
"Unspecified");
String course = (String) [Link]();
String rating = [Link]();
String modules = "";
if ([Link]()) modules += "Practical ";
if ([Link]()) modules += "Theory";
[Link]("<html>Name: " + name + "<br>Gender: " + gender +
"<br>Course: " + course + "<br>Rating: " + rating +
"<br>Modules: " + modules + "</html>");
});
[Link](350, 400);
[Link](null);
[Link](true);
[Link](JFrame.EXIT_ON_CLOSE);
}
}