Event Handling using Inner Class in 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

Posted

in

by


Related Posts

Comments

Leave a Reply

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

Discover more from Jitendra Zaa

Subscribe now to keep reading and get access to the full archive.

Continue Reading