Home Java Example Java Awt Event Adapters
Questions:Ask|Latest

 
 

Share on Google+Share on Google+

Event Adapters

Advertisement
There are some event listeners that have multiple methods to implement. That is some of the listener interfaces contain more than one method.

Event Adapters

     

There are some event listeners that have multiple methods to implement. That is some of the listener interfaces contain more than one method. For instance, the MouseListener interface contains five methods such as mouseClicked, mousePressed, mouseReleased etc. If you want to use only one method out of these then also you will have to implement all of them. Thus, the methods which you do not want to care about can have empty bodies. To avoid such thing, we have adapter class. 

Adapter classes help us in avoiding the implementation of the empty method bodies. Generally an adapter class is there for each listener interface having more than one method. For instance, the MouseAdapter class implements the MouseListener interface. An adapter class can be used by creating a subclass of it and then overriding the methods which are of use only. Hence avoiding the implementation of all the methods of the listener interface. The following example shows the implementation of a listener interface directly. 

public class MyClass implements MouseListener {
...
someObject.addMouseListener(this);
...

/* Empty method definition. */
public void mouseEntered(MouseEvent e) {
}

/* Empty method definition. */
public void mouseExited(MouseEvent e) {
}

/* Empty method definition. */
public void mousePressed(MouseEvent e) {
}

}
}

 

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: December 17, 2008

Related Tutorials

Ask Questions?    Discuss: Event Adapters  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
muhammed younus attari
March 28, 2011
i like it

i like this website, it helped me allot but the thing is if it provides examples then it will be more good.
deepak kumar sana
January 23, 2013
core java

something ??????????????
DMCA.com