Java - Applet - Event handling

Event handling in applets is the process of writing code to respond to different types of events that may occur within the applet. Events are generated when the user interacts with the applet by clicking on buttons, typing in text fields, or performing other actions.

In order to handle events in an applet, you need to follow these steps:

  • Create an event listener: An event listener is an object that is responsible for handling a particular type of event. You can create an event listener by implementing one of the many listener interfaces provided by Java, such as ActionListener or MouseListener.
  • Register the listener: Once you have created the listener, you need to register it with the component that generates the event. For example, if you want to handle button clicks, you need to register your ActionListener with the button component.
  • Implement the event handling code: Once the event listener is registered, you need to implement the code that responds to the event. This code will typically be contained in a method that is defined in the event listener interface. For example, if you are handling button clicks, you will need to implement the actionPerformed() method of the ActionListener interface.

Here is an example of how to handle button clicks in an applet:

import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class ButtonDemo extends Applet implements ActionListener {
    Button b;
    Label l;

    public void init() {
        b = new Button("Click me!");
        l = new Label("");
        add(b);
        add(l);
        b.addActionListener(this);
    }

    public void actionPerformed(ActionEvent e) {
        l.setText("Button clicked!");
    }
}

In this example, we create a simple applet that contains a button and a label. We then register an ActionListener with the button using the addActionListener() method. When the button is clicked, the actionPerformed() method of the ActionListener interface is called, which sets the text of the label to "Button clicked!".

You can use this same basic approach to handle other types of events, such as mouse clicks or keyboard input.

Mouse events: You can use the MouseListener and MouseMotionListener interfaces to handle mouse events in your applet. For example, the following code changes the color of a rectangle when the mouse is clicked on it:

import java.awt.*;
import java.awt.event.*;

public class MyMouseApplet extends java.applet.Applet implements MouseListener {
   int x = 50, y = 50, width = 100, height = 100;
   Color rectColor = Color.blue;

   public void init() {
      addMouseListener(this);
   }

   public void paint(Graphics g) {
      g.setColor(rectColor);
      g.fillRect(x, y, width, height);
   }

   public void mouseClicked(MouseEvent e) {
      if (x <= e.getX() && e.getX() <= x + width && y <= e.getY() && e.getY() <= y + height) {
         rectColor = Color.red;
         repaint();
      }
   }

   public void mouseEntered(MouseEvent e) {}
   public void mouseExited(MouseEvent e) {}
   public void mousePressed(MouseEvent e) {}
   public void mouseReleased(MouseEvent e) {}
}

Keyboard events: You can use the KeyListener interface to handle keyboard events in your applet. For example, the following code changes the color of a rectangle when the user presses a key:

import java.awt.*;
import java.awt.event.*;

public class MyKeyboardApplet extends java.applet.Applet implements KeyListener {
   int x = 50, y = 50, width = 100, height = 100;
   Color rectColor = Color.blue;

   public void init() {
      addKeyListener(this);
      setFocusable(true);
   }

   public void paint(Graphics g) {
      g.setColor(rectColor);
      g.fillRect(x, y, width, height);
   }

   public void keyPressed(KeyEvent e) {
      if (e.getKeyCode() == KeyEvent.VK_SPACE) {
         rectColor = Color.red;
         repaint();
      }
   }

   public void keyReleased(KeyEvent e) {}
   public void keyTyped(KeyEvent e) {}
}

Action events: You can use the ActionListener interface to handle action events in your applet. For example, the following code displays a message dialog when a button is clicked:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class MyButtonApplet extends java.applet.Applet implements ActionListener {
   JButton button;

   public void init() {
      button = new JButton("Click me!");
      add(button);
      button.addActionListener(this);
   }

   public void actionPerformed(ActionEvent e) {
      JOptionPane.showMessageDialog(null, "Button clicked!");
   }
}