Advertisement



< Prev
Next >



Event Handling in Applets





An event in a java program is generated upon clicking a button, typing in a textfield, checking a checkbox or radio box, selecting an item in a drop down list etc. In this article we will understand how such events are handled, when generated from within an applet window.




Note :


In order to handle such events, we need to register our program to listen such events when they are generated by our program, during its execution.


Advertisement




How do we register our program to listen events generated by it?


To listen events, we must implement interfaces related to the kind of event that is generated by our program. For example, if our program has a button in it and in order to listen a button click event, we must implement ActionListener interface and implement its method actionPerformed(), which gives us the name of the button that was clicked, that lead to a button click event.




Event handling in Applet


In the forthcoming code, we have created an applet named Applet2 by extending the Applet class, besides this we have also implemented -
  • ActionListener interface and have implemented its actionPerformed() method to listen to button click events or when an Enter key is pressed in a textfield within our program.
  • ItemListener interface and have implemented its itemStateChanged() method to listen to checking/unchecking of checkboxes or radio boxes in our program.

Applet2.java
//Evening handling in an applet 

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

/*
<applet code="Applet2" width=250 height=200>
*lt;/applet>
*/

public class Applet2 extends Applet implements ActionListener, ItemListener
{
Label label1, label2, label3; 
TextField tf1, tf2, tf3;
Checkbox ck1, ck2;
CheckboxGroup cb;


public void init()
{
System.out.println("Initializing an applet");

label1 = new Label("Enter your name");
tf1= new TextField(10);

label2 = new Label("Enter your city");
tf2= new TextField(10);

label3 = new Label("Gender");

cb= new CheckboxGroup();
ck1 = new Checkbox("Male",cb,false);
ck2 = new Checkbox("Female",cb,false);

add(label1);			//adding first label to the applet window.
add(tf1);			//adding first textfield to the applet window.

add(label2);			//adding second label to the applet window.
add(tf2);			//adding second textfield to the applet window.

add(label3);			//adding third label to the applet window.
add(ck1);			//adding the first checkbox to the applet window
add(ck2);			//adding the second checkbox to applet window

tf1.addActionListener(this); 	//Applet2 class registering to listen to textfield events
tf2.addActionListener(this); 	//Applet2 class registering to listen to textfield events
ck1.addItemListener(this);	//Applet2 class registering to listen to checkbox events
ck2.addItemListener(this);	//Applet2 class registering to listen to checkbox events
}


public void actionPerformed(ActionEvent ae) //listening to textfields events when enter is pressed in a textField
{
repaint();	//repaint() calls paint() method when a button click event is generated.
}

public void itemStateChanged(ItemEvent ie) //Listening to checkboxes when a checkbox is selected
{
repaint();	//repaint() calls paint() method when a checkbox is checked or unchecked.
}


public void paint(Graphics g)
{
g.drawString("Your name is "+ tf1.getText(),  10, 150 );
g.drawString("Your city is "+ tf2.getText(), 10,170 );
if(cb.getSelectedCheckbox()!=null)
{
g.drawString("Your gender is "+ cb.getSelectedCheckbox().getLabel(), 10,190);
}
}
}


Output

In order to run our applet using appletviewer, type the following command at command prompt-

appletviewer Applet2.java


Where Applet2.java is the name of java file that contains the code of an applet. Right after running the applet program using appletviewer a new applet window is displayed to us -



Note : For an applet class that extends AWT's Applet class, the way elements like textfield, buttons, checksboxes are added to this applet is performed by using default layout manager, i.e. FlowLayout.


Program Analysis


    In our code two kinds of events could be generated -
  • Whenever we press the Enter key while entering the information in textfield, an ActionEvent is generated and actionPerformed() method is called, which calls repaint() method, which in turns calls the paint() method defined in our Applet class, to display the name or city you've entered.
  • Whenever a checkbox is checked or unchecked an ItemEvent is generated and itemStateChanged() method is called, which calls repaint() method. which in turn calls the paint() method defined in our applet class, to show the gender you've selected in the applet.




Please share this article -



Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement

Notifications



Please check our latest addition

PYTHON


Advertisement

p