๐Ÿš€ AWT Event Handling & User Input Processing (Extension of AWT GUI Blog)


๐ŸŒŸ 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 user
  • Integer.parseInt() โ†’ converts String to int
  • ActionListener โ†’ handles button click
  • setText() โ†’ 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 ๐Ÿ”ฅ

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *