How to Create an Event Listener in Applet?
Last Updated :
23 Oct, 2023
In this article, we will be creating the event Listener in the Applet window. Here, we will use the ActionListener on Button and MouseListener when the event has occurred through mouse activities.
Approach for Implementing Event Listener Applet
- We need to import the essential packages like Applet and AWT for customization.
- When the packages get imported, then we need to create the class that implemented the ActionListener and MouseListener interface in Java.
- When the class gets created, then we need to initiate and create the GUI components which will be displayed on the Applet Window. These components are Buttons, Messages, etc.
- After the initialization of the components is done, we need to define the init() method, which will be used to make the components applied on the Applet window by customizing their colors, font, size, etc.
- Then mainly, we will apply the event listeners like actionPerformed() on the button. If the button is been clicked, the shape is been filled with a random color.
- Additionally, there is MouseListener, which has various methods to perform events like mouseClick, mouseEntered, mouseExited, etc.
Steps to Implement EventListener Applet
The folder view of the project is shown in the below image.

Step 1: We need to check the Java version by running the below command in the terminal:

Step 2: First, create the file as ListenerApplet.java and enter the code into it.
Step 3: Once the code is been inserted then we need to create the ListenerApplet.html file, through which we will display the output.
HTML
<!DOCTYPE html>
<html>
<head>
<title>Listener Applet</title>
</head>
<body>
<applet code="ListenerApplet.class" width="300" height="300">
</applet>
</body>
</html>
Step 4: Then we need to compile the Java code using the below command. We can see that the new .class file is been generated.
javac ListenerApplet.java

Step 5: Now finally we need to run the application using the below command:
appletviewer ListenerApplet.html

Program to Create Event Listener using Applet
Implementation of the ListenerApplet program is mentioned below:
Java
// Java Program to create an event listener in Applet
import java.applet.Applet;
import java.awt.Button;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.Panel;
//Driver Class
public class ListenerApplet extends Applet implements ActionListener, MouseListener {
private String message = "Click, hover";
private Color shapeColor = Color.WHITE;
public void init() {
// Creating a panel to hold the btn
Panel cp = new Panel();
// Creating a btn with an ActionListener
Button btn = new Button("Change Color");
Font buttonFont = new Font("Arial", Font.BOLD, 18);
btn.setFont(buttonFont);
btn.setBackground(new Color(50, 100, 150));
btn.setForeground(Color.WHITE);
btn.addActionListener(this);
// Adding the btn to the control panel
cp.add(btn);
// Registering MouseListener
addMouseListener(this);
// Adding the control panel to the applet
add(cp);
}
public void actionPerformed(ActionEvent e) {
// Changing the shape color on btn click
shapeColor = shapeColorFn();
repaint();
}
public void mouseClicked(MouseEvent e) {
// Displaying a message when the applet is clicked
message = "Mouse Clicked at (" + e.getX() + ", " + e.getY() + ")";
repaint();
}
public void mouseEntered(MouseEvent e) {
// Changing the message when the mouse enters the applet
message = "Mouse Entered!";
repaint();
}
public void mouseExited(MouseEvent e) {
// Changing the message when the mouse exits the applet
message = "Mouse Exited!";
repaint();
}
public void mousePressed(MouseEvent e) {
// Empty method required by MouseListener
}
public void mouseReleased(MouseEvent e) {
// Empty method required by MouseListener
}
public void paint(Graphics g) {
int centerX = getWidth() / 2;
int centerY = getHeight() / 2;
// Drawing a shape with the chosen color
g.setColor(shapeColor);
g.fillRoundRect(centerX - 100, centerY - 50, 200, 100, 20, 20);
// Drawing the message text
g.setFont(new Font("Arial", Font.PLAIN, 18));
g.setColor(Color.BLACK);
g.drawString(message, 10, 40);
}
// Method to generate a random color
private Color shapeColorFn() {
int red = (int) (Math.random() * 256);
int green = (int) (Math.random() * 256);
int blue = (int) (Math.random() * 256);
return new Color(red, green, blue);
}
}
Output for the program:

Explanation of the Program of Event Listener Applet
- We have imported the packages that are necessary to define the Applet and the AWT components like Buttons, Font, Color, etc.
- Then we have defined the main ListenerApplet class which is been implemented in the ActionListener and MouseListener interface.
- There is an init() method that is used for the initialization of the components like font, Buttons, and Color and adds it into the Applet window by applying the customization properties of Color, font, Size, etc.
- Then we have the actionPerformed() method which is of the ActionListener interface. This method is used to perform the actions as per the click on the Button.
- As we have also defined the MouseListener, we have used different methods of this listener like mouseclick(), mouseEntered(), mousePressed(), etc. All these methods are used to handle the events that occur by the mouse clicks or the movements of the Mouse.
- There is a user-defined method called shapeColorFn() that is used to generate the random color and apply it on each button click to the Shape.
Similar Reads
How to create an event in React ? In the development of contemporary webpages, user interactions play a pivotal role. These interactions include mouse clicks, keypresses, or even uncommon events like connecting a battery to a charger. As developers, our task is to actively "listen" to these events and subsequently ensure that our ap
2 min read
Event Listeners Tab in Google Chrome Browser The Event Listeners Tab is present in the Elements tool in Chrome and is used to check various event listeners of the elements present in the web page. You get various options, such as filter, refresh, include ancestors, and many more, in the context menu.Table of ContentBenefits of the Event Listen
4 min read
Servlet - Context Event and Context Listener ServletContextEvent class provides alerts/notifications for changes to a web application's servlet context. ServletContextListener is a class that receives alerts/notifications about changes to the servlet context and acts on them. When the context is initialized and deleted, the ServletContextListe
3 min read
Servlet - Event and Listener The term "event" refers to the occurrence of something. An event occurs when the state of an object changes. When these exceptions occur, we can conduct certain crucial actions, such as collecting total and current logged-in users, establishing database tables when deploying the project, building da
8 min read
Difference between Applets and Servlets Prerequisite: Servlets and Applets AppletsServletsA Java applet is a small application which is written in Java and delivered to users in the form of bytecode.A servlet is a Java programming language class used to extend the capabilities of a server.Applets are executed on client side.Servlets are e
2 min read
Java Program to Create Different Shapes using Applet In this article, we will be creating different shapes using Applet. Here, we have taken an Input field, where the user can enter the name of the shape and click on the button. After clicking on the button, the shape that is entered will get drawn on the Applet window. OverviewInitially, we have to i
4 min read