We can handle the Event in Applet, AWT or Swing by following three different methods:
- Inner Class
- Anonymous class
- this Pointer
In this example, i will show you that how to use inner classes to handle the events in Applet.
package com.kunal.applet;
import java.applet.Applet;
import java.awt.Button;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class InerClassButton extends Applet {
TextField txtSource;
Button btnLower,btnUpper;
String lowerText="lower case";
public void init()
{
txtSource=new TextField(20);
btnLower=new Button(lowerText);
btnUpper=new Button("upper case");
btnLower.addActionListener(new MyActionListener());
btnUpper.addActionListener(new MyActionListener());
add(txtSource);
add(btnLower);
add(btnUpper);
}
class MyActionListener implements ActionListener
{
@Override
public void actionPerformed(ActionEvent evt) {
String tmp=txtSource.getText();
txtSource.setText(evt.getActionCommand()==lowerText?tmp.toLowerCase():tmp.toUpperCase());
}
}
}

Leave a Reply