๐ Introduction
In the previous AWT blog, we learned how to:
- Create GUI windows ๐ช
- Add components like Button, Label, TextField
- Use Layout Managers ๐ฏ
๐ But there was one big missing pieceโฆ
โ How do we take input from the user and actually process it?
This blog solves that problem ๐ก
๐ง What You Will Learn
- How to read input from
TextField - How to handle button click events
- How to process input (like Even/Odd, Sum, etc.)
- How GUI becomes interactive ๐ฅ
๐ Event Handling in AWT (Basic Idea)
๐ When a user clicks a button:
- An event is generated โก
- We must handle it using ActionListener
๐งฑ Basic Structure of Event Handling
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// Code to execute on button click
}
});
๐งช Program 1: Even or Odd Checker
โ Complete Runnable Program
import java.awt.*;
import java.awt.event.*;
public class EvenOddAWT {
public static void main(String[] args) {
Frame frame = new Frame("Even Odd Checker");
frame.setLayout(new FlowLayout());
Label label = new Label("Enter Number:");
TextField tf = new TextField(10);
Button btn = new Button("Check");
Label result = new Label("");
frame.add(label);
frame.add(tf);
frame.add(btn);
frame.add(result);
btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
int num = Integer.parseInt(tf.getText());
if (num % 2 == 0) {
result.setText("Even Number โ
");
} else {
result.setText("Odd Number ๐ฅ");
}
} catch (Exception ex) {
result.setText("Invalid Input โ");
}
}
});
frame.setSize(300, 200);
frame.setVisible(true);
}
}
๐ Explanation
tf.getText()โ reads input from userInteger.parseInt()โ converts String to intActionListenerโ handles button clicksetText()โ displays output
๐งช Program 2: Addition of Two Numbers
โ Complete Runnable Program
import java.awt.*;
import java.awt.event.*;
public class AdditionAWT {
public static void main(String[] args) {
Frame frame = new Frame("Addition Calculator");
frame.setLayout(new FlowLayout());
TextField tf1 = new TextField(10);
TextField tf2 = new TextField(10);
Button btn = new Button("Add");
Label result = new Label("Result: ");
frame.add(new Label("Number 1:"));
frame.add(tf1);
frame.add(new Label("Number 2:"));
frame.add(tf2);
frame.add(btn);
frame.add(result);
btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
int n1 = Integer.parseInt(tf1.getText());
int n2 = Integer.parseInt(tf2.getText());
int sum = n1 + n2;
result.setText("Result: " + sum);
} catch (Exception ex) {
result.setText("Invalid Input โ");
}
}
});
frame.setSize(300, 200);
frame.setVisible(true);
}
}
๐งช Program 3: Factorial Calculator
โ Complete Runnable Program
import java.awt.*;
import java.awt.event.*;
public class FactorialAWT {
public static void main(String[] args) {
Frame frame = new Frame("Factorial Calculator");
frame.setLayout(new FlowLayout());
TextField tf = new TextField(10);
Button btn = new Button("Find Factorial");
Label result = new Label("");
frame.add(new Label("Enter Number:"));
frame.add(tf);
frame.add(btn);
frame.add(result);
btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
int num = Integer.parseInt(tf.getText());
int fact = 1;
for (int i = 1; i <= num; i++) {
fact *= i;
}
result.setText("Factorial: " + fact);
} catch (Exception ex) {
result.setText("Invalid Input โ");
}
}
});
frame.setSize(300, 200);
frame.setVisible(true);
}
}
๐งช Program 4: Reverse a Number
โ Complete Runnable Program
import java.awt.*;
import java.awt.event.*;
public class ReverseNumberAWT {
public static void main(String[] args) {
Frame frame = new Frame("Reverse Number");
frame.setLayout(new FlowLayout());
TextField tf = new TextField(10);
Button btn = new Button("Reverse");
Label result = new Label("");
frame.add(new Label("Enter Number:"));
frame.add(tf);
frame.add(btn);
frame.add(result);
btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
int num = Integer.parseInt(tf.getText());
int rev = 0;
while (num != 0) {
int digit = num % 10;
rev = rev * 10 + digit;
num /= 10;
}
result.setText("Reverse: " + rev);
} catch (Exception ex) {
result.setText("Invalid Input โ");
}
}
});
frame.setSize(300, 200);
frame.setVisible(true);
}
}
How to make the program close by clicking on the close button of the frame:-
import java.awt.*;
import java.awt.event.*;
public class CloseWindowDemo {
public static void main(String[] args) {
Frame frame = new Frame("Close Example");
frame.setSize(300, 200);
// Handling window closing event
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0); // Terminates program
}
});
frame.setVisible(true);
}
}
โ ๏ธ Common Mistakes Students Make
โ Forgetting getText()
๐ You cannot directly use TextField value
โ Not converting String to int
int num = tf.getText(); // โ WRONG
โ Not handling invalid input
๐ Program crashes if user enters text
โ Forgetting to add ActionListener
๐ Button will not work
๐คฏ Common Confusions
๐ค Why input is String?
๐ Because GUI components deal with text
๐ค Why use ActionListener?
๐ To respond to user actions (clicks)
๐ผ Interview Questions
โ How to read input from TextField?
๐ Using getText()
โ How to handle button click?
๐ Using ActionListener
โ Why convert String to int?
๐ Because TextField returns String
๐ฏ Key Takeaways
โ๏ธ GUI input is always String
โ๏ธ Use getText() to read input
โ๏ธ Use parseInt() to convert
โ๏ธ Use ActionListener for interaction
๐ Conclusion
Now your GUI is no longer static โ it is interactive ๐ฏ
You learned:
- โ๏ธ How to take input from user
- โ๏ธ How to process data
- โ๏ธ How to handle events
- โ๏ธ Building real AWT applications
๐ก Final Thought
๐ This is the bridge between basic GUI and real applications ๐
๐ฅ One-Line Summary
๐ โAWT becomes powerful when user input and event handling are combined.โ
๐ป Happy Coding! ๐
Next step: Event Delegation Model (deep dive) & Swing ๐ฅ