0% found this document useful (0 votes)
124 views

Adapter Classes

Java provides adapter classes to simplify the creation of event handlers. Adapter classes provide empty implementations of all methods in a listener interface. This allows developers to only implement the event methods they are interested in, rather than all methods. Some common adapter classes include WindowAdapter, KeyAdapter, and MouseAdapter which correspond to the WindowListener, KeyListener, and MouseListener interfaces.

Uploaded by

Alok
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
124 views

Adapter Classes

Java provides adapter classes to simplify the creation of event handlers. Adapter classes provide empty implementations of all methods in a listener interface. This allows developers to only implement the event methods they are interested in, rather than all methods. Some common adapter classes include WindowAdapter, KeyAdapter, and MouseAdapter which correspond to the WindowListener, KeyListener, and MouseListener interfaces.

Uploaded by

Alok
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Adapter Classes

Java provides a special feature, called an adapter class, which will simplify the creation of event handlers in certain situations. An adapter class provides an empty implementation of all
methods in an occasion listener interface. Adapter classes are useful once you want to receive and process just some of the events that are handled by a specific event listener interface.
You can define a replacement class to act as an occasion listener by extending one among the adapter classes and implementing only those events during which you’re interested.

java.awt.event Adapter classes

Adapter class Listener interface

WindowAdapter WindowListener

KeyAdapter KeyListener

MouseAdapter MouseListener

MouseMotionAdapter MouseMotionListener

FocusAdapter FocusListener

ComponentAdapter ComponentListener

ContainerAdapter ContainerListener

HierarchyBoundsAdapter HierarchyBoundsListener

import java.awt.*;
import java.awt.event.*;
public class MouseAdapterExample extends MouseAdapter{
Frame f;
MouseAdapterExample(){
f=new Frame("Mouse Adapter");
f.addMouseListener(this);

f.setSize(300,300);
f.setLayout(null);
f.setVisible(true);
}
public void mouseClicked(MouseEvent e) {
int x=m.getX();
int y=m.getY();
t1.setText(x+","+y);
}

public static void main(String[] args) {


new MouseAdapterExample();
}
}
Anonymous Inner Class
import java.awt.*;
import java.awt.event.*;
class MouseAnonymousDemo
{
Label l1;
TextField t1;
Frame f;
MouseAnonymousDemo()
{
f=new Frame();
f.setTitle("My First Frame");
f.setSize(500,500);
f.setVisible(true);
f.setLayout(new FlowLayout());
l1=new Label("Current Mouse Clicked Position");
t1=new TextField(10);
f.add(l1);
f.add(t1);
f.addMouseListener( new MouseAdapter() {
public void mouseClicked (MouseEvent m)
{
int x=m.getX();
int y=m.getY();
t1.setText(x+","+y);
}

});

}
}

class MouseAnonymousFrame
{
public static void main(String arg[])
{
MouseAnonymousDemo m =new MouseAnonymousDemo();

}
}

You might also like