Listener and Adapter Listener&adapter

Source: Internet
Author: User

Why do I need adapter? Short answer: Simplifying

"Event Source" can register (addxxxlistener) Listener object and Send event object

WindowListener listener = ...; Frame.addwindowlistener (listener);

Specific event corresponding to a specific listener interface (Listener interface);

 Public Interface windowlistener{   void  windowopened (windowevent e);    void windowclosing (WindowEvent e);}

In order to handle a particular event, the listener implements the corresponding interface , which in Java means all methods (windowopened, windowclosing) that need to implement the interface . )
But the listener may only be interested in one of these methods, and other methods don't need to do anything, so it can be tedious to implement:

class Implements windowlistener{    publicvoid  windowclosing (windowevent e) {        if  (user agrees)             System.exit (0);    }      Public void windowopened (WindowEvent e) {}    ...}

To simplify, each AWT listener interface with multiple methods has an adapter (Adapter) class that implements all the methods in the interface, but each method does nothing. Need to construct monitoring
Terminator_b, you only need to extend (extends) this adapter and overriding the function of interest. :

In practice, when you create a listener class, we define the listener as an anonymous intrinsic to the event source, as follows:

Frame.addwindowlistener (new  windowadapter () {     publicvoid  Windowclosing (WindowEvent e) {          if(user agrees)              System.exit (0);     }});

Listener and Adapter Listener&adapter

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.