Event Handling using Inner Class in Applet

Author posted by Jitendra on Posted on under category Categories JAVA and tagged as Tags , , with Leave a comment on Event Handling using Inner Class in Applet

Demonstrates the example for handling event using Inner Class in Java Applet

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());
     }
   }
}

Event Handling using Inner Class in Applet

Related posts

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.