Anonymous Inner class with Listeners Adapters

Anonymous inner classes were introduced, at the first, to make event handling simpler and easier. With inner classes, forget the laborious days of implementing listeners and extending adapters classes. Now the code comes right at the place of linking the listener with the component in the constructor itself.

Two programs are given (using Anonymous Inner class with Listeners Adapters) one on window closing and one on button event handling using anonymous inner classes.

I. Anonymous inner class with Adapter

In the following program, frame is closed using anonymous inner class of WindowListener

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

public class SimpleFrameClosing extends Frame    // No implements WindowListener
{                                                                                  // to close the Frame
  public SimpleFrameClosing()
  {
    addWindowListener(new WindowAdapter()
    {
      public void windowClosing(WindowEvent e)
      {
        System.out.println("Thank U, Window is Now Closed");
        System.exit(0);
      }
    }
    );
    
    setSize(300, 300);
    setVisible(true);
  }
  public static void main(String args[])
  {
    new SimpleFrameClosing();
   }
}


Anonymous Inner class with Listeners Adapters
Output screen of Anonymous Inner class with Listeners Adapters

addWindowListener() is a method of class Window inherited by Frame. Here "new WindowAdapter()" is an anonymous class of WindowAdapter.

II. Anonymous inner class with Listener

In the following code, button event handling is done with anonymous inner class of ActionListener.

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

public class AdapterWithAnonymous extends Frame      // No implements ActionListener
{
  Button btn;
  public AdapterWithAnonymous()
  {
    setLayout(new FlowLayout());
  
    btn = new Button("Click for Red");
    add(btn);

    btn.addActionListener(new ActionListener()
    {
      public void actionPerformed(ActionEvent e)
      {
        System.out.println("Your click is Successful");
        setBackground(Color.red);
      }
    }
    );
    
    setSize(300, 300);
    setVisible(true);
  }
  public static void main(String args[])
  {
    new AdapterWithAnonymous();
   }
}


Anonymous Inner class with Listeners Adapters
Output screenshot on Anonymous Inner class with Listeners Adapters

addActionListener() is a method of class Button. Here "new ActionListener()" is an anonymous inner class of ActionListener. The event handling code comes in the constructor itself.

3 thoughts on “Anonymous Inner class with Listeners Adapters”

  1. can you write the program for the following:
    write a program to display a frame. in addition you need to handle the movement of the mouse by using an anonymous inner class, so that the position of the mouse pointer is displayed by a label inside the frame

  2. //i cant understand the error of the program can u help me
    import java.awt.event.*;
    import java.awt.*;
    import java.applet.*;
    /*

    */
    public class Cirdemo extends Applet
    {
    int x,y;
    public void init()
    {
    addMouseMostionListener(new MouseMostionAdaptor()
    {
    public void mouseDragged(MouseEvent me)
    {
    x=me.getX();
    y=me.getY();
    repaint();
    }
    });
    }
    public void paint(Graphics g)
    {
    g.setColor(Color.red);
    g.fillArc(x,y,90,90,0,360);
    }
    }
    //i need help

Leave a Comment

Your email address will not be published.