Merlin's Magic: Dynamic Event Listener Agent

Source: Internet
Author: User
Tags anonymous event listener reflection

All Swing components are JavaBeans components. They have a series of setter and Getter methods that are similar to the void Setxxx (type name) and type GetXXX (). There is nothing special about these methods, and as expected, they follow the JavaBeans attribute naming conventions. What we're going to discuss today is one aspect of the JavaBeans component, a pair of listener methods Addxxxlistener (xxxlistener name) and Removexxxlistener (xxxlistener name). Xxlistener here refers to a listener object that extends the EventListener interface and waits for various events to occur in the component associated with the listener. When an event occurs, all registered listeners will be notified of the event (in no particular order). With a magic little reflection (reflection) and a new Java.beans.EventHandler class, you can attach a listener to a bean without having to implement the listener interface directly or create annoying little anonymous inner classes.

The previous approach

Before delving into the details of using the new EventHandler class, let's review how it works without using this class. Let's give a simple example of a response to a button selection in a Swing frame. Select a button to generate a actionevent. To respond to this event, you need to attach actionlistener to this button, as shown in Listing 1:

Listing 1. Monitor Standard button selection

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ButtonSelection extends JFrame {
  public ButtonSelection() {
   super("Selection");
   setDefaultCloseOperation(EXIT_ON_CLOSE);
   JButton button = new JButton("Pick Me");
   Container contentPane = getContentPane();
   contentPane.add(button, BorderLayout.CENTER);
   button.addActionListener(
    new ActionListener() {
     public void actionPerformed(ActionEvent e) {
      System.out.println("Hello, World!");
     }
    }
   );
  }
  public static void main(String args[]) {
   JFrame frame = new ButtonSelection();
   frame.setSize(200, 100);
   frame.show();
  }
}

There's nothing magical about this, you're probably already familiar with this code. Here, the ActionListener implementation is defined in a timely manner, defined as an anonymous inner class and attached directly to the button. When this button is selected, the string Hello, world! is printed to the console. The screen associated with the program is shown in Figure 1:

Figure 1. Button selection with ActionListener

In the JavaBeans specification, you are not required to create anonymous inner classes for event sniffing. IDE tools often use this behavior: You say you want a listener, it generates a stub, and then you fill in the details. Other ways to accomplish the same work include providing the specified implementation in the calling class or implementing your own interface.

Once each implementation class is defined, a separate. class file is created. So, in the previous Buttonselection program, you'll see that the compiler generates two. class files: Buttonselection.class and Buttonselection$1.class. The way the Sun compiler names anonymous inner classes, the count increases with each class. Other compilers may have different naming methods.

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.