Event Handling Using Applet In Java

Introduction

 
In this article, we discuss event handling in Java Applets.
 

Event Handling in Applet

 
We perform event handling in Swing and AWT and we can also do it in Applets.
 
As we know, Graphical Programming Interfaces (GUIs) contain the components of the user interface. The GUI components are responsible to generate events based on user interactions like clicking the mouse or a key and so on. When an applet is designed, these events are captured and the appropriate actions are performed in response to each of those events provided.
 
For handling events, we need event handlers, that must be available in Java. The procedure to be followed when an event is generated is:
  1. First, we must find the type of event that took place.
  2. Now, find the component that generated the event.
  3. Finally, we need the appropriate code that handles the event.

Button event handling

 
It is the most common event generating components in user interfaces. When we click on a button, an action is generated. In fact, the term action is used in button event handling.
 
Properties
 
Some of the properties of a button event are:
  • It manages the list of button objects that implement the ActionListener interface.
  • The actionPerformed method is invoked when we click or press on a button, it is invoked for all of the ActionListener instances in its list.
  • The abstract actionPerformed method overrides the ActionListener classes to perform the task for the button.
  • As an argument of actionPerformed, an ActionEvent object is passed and from it you can extract information about the event such as which ActionListener initiated the event.
  • To add an instance of ActionListener to a button, we must invoke its addActionListener(ActionListener) method.
The following example will help in the understanding of event handling in Applets.
 
Example
 
In this example, we perform event handling in an Applet that prints a message by clicking on the button.
  1. import java.awt.event.*;  
  2. import java.applet.*;  
  3. import java.awt.*;  
  4. public class EventApplet extends Applet implements ActionListener  
  5. {  
  6.     Button bttn;  
  7.     TextField txtfld;  
  8.     public void init()  
  9.     {  
  10.         txtfld=new TextField();  
  11.         txtfld.setBounds(35,45,250,30);  
  12.         bttn=new Button("click me");  
  13.         bttn.setBounds(90,110,70,60);  
  14.         add(bttn);add(txtfld);  
  15.         bttn.addActionListener(this);  
  16.         setLayout(null);  
  17.       }  
  18.       public void actionPerformed(ActionEvent ae)  
  19.       {  
  20.         txtfld.setText("welcome to c-sharpcorner.com");  
  21.       }  
  22. }  
  23. /* 
  24. <applet code="EventApplet.class" width="400" height="400"> 
  25. </applet 
  26. */  
Output
 
fig-1.jpg
 
When we click on "click me" button an event is generated that displays the message that we pass in our program. The output is shown below.
 
fig-2.jpg
 

JApplet class in Applet

 
Since Swing is more attractive than AWT, we can use JApplet that has all the controls that Swing does. The JApplet class extends the Applet class.
 
Example
 
In this example, we show EventHandling using JApplet.
 
The following is similar to the previous example, but we see that there is a little difference between the both since in this example we are using the Swing component, so it looks more attractive as you can see below.
  1. import java.awt.event.*;  
  2. import java.applet.*;  
  3. import javax.swing.*;  
  4. public class AppletJEvent extends Applet implements ActionListener  
  5. {  
  6.     JButton bttn;  
  7.     JTextField txtfld;  
  8.     public void init()  
  9.     {  
  10.         txtfld = new JTextField();  
  11.         txtfld.setBounds(354525030);  
  12.         bttn = new JButton("click me");  
  13.         bttn.setBounds(901107060);  
  14.         add(bttn);  
  15.         add(txtfld);  
  16.         bttn.addActionListener(this);  
  17.         setLayout(null);  
  18.     }  
  19.     public void actionPerformed(ActionEvent ae)  
  20.     {  
  21.         txtfld.setText("welcome to c-sharpcorner.com");  
  22.     }  
  23. }  
  24. /* 
  25. <applet code="AppletJEvent.class" width="400" height="400"> 
  26. </applet 
  27. */  
Output
 
fig-3.jpg
 
When we click on the "click me" button an event is generated that displays the message that we pass in our program. The output is shown below.
 
fig-4.jpg


Similar Articles