Java listens to events that are easy to use.

Source: Internet
Author: User

Java listens to events that are easy to use.

The first time I heard that the Listener was a webGIS project three years ago. At that time, my impression on Listener was a "Monitor", which monitored every action on the interface and triggered a corresponding response when there was a dynamic activity.

I. Overview

By adding a listener to one or more operations on the interface, you can call the listener function/listener class spontaneously to respond to the operation. For example, the monitored operation is "you make your mom angry", and the added response is "your mom called your dad and your dad went home, your dad beat you ". So no matter what time, as long as "you make your mom angry", it will trigger the listening of this operation. The final result is that you will be beaten by your dad every time. It has been tried and tested, without exception. This is the event listener, java. util. EventListener.

Ii. Types of listener events

There are many Listener types, but I cannot sum up a macro framework. Here I will only list the commonly used CS listeners. I will leave them for future Supplements: ActionListener, MouseListener, PopMenuListener, windowListener, and so on.


3. Listener event implementation method 1. Add a listener directly to the control

For example, add an ActionListener listener to the Save button.

1         saveCharaEditButton.addActionListener(new ActionListener() {2             3             @Override4             public void actionPerformed(ActionEvent e) {5                 // TODO Auto-generated method stub6                 save();7             }8         });
2. Write a listener class to implement the listener interface.

When you add an event listener, you only need to add this listener class. [PS: This internal class can be written in parallel with the function. You do not need to create another class file, but do not add a public modifier before. By default, only one public class is allowed .]

1 public JPanel setRightPanel () {2 JPanel rightPanel = new JPanel (); 3 double [] [] size = {15, TableLayout. FILL, 30, TableLayout. FILL, 35}, {10, 30, 10, TableLayout. FILL }}; 4 rightPanel. setLayout (new TableLayout (size); 5 JButton saveCharaEditButton = new JButton ("Save edit"); 6 JButton enableCharaEditButton = new JButton ("Enable Edit"); 7 rightPanel. add (enableCharaEditButton, "1, 1"); 8 rightPanel. add (saveCharaEditButton, "3, 1"); 9 rightPanel. add (setCharaJCTablePanel (), "0, 3, 4, 3"); 10 saveCharaEditButton. addActionListener (new saveEditToFloodCharacterValueListener (); // Add listening 11 return rightPanel; 12}
 1     class saveEditToFloodCharacterValueListener implements ActionListener{ 2  3         @Override 4         public void actionPerformed(ActionEvent e) { 5             // TODO Auto-generated method stub 6             FloodCharacterValue floodCharacterValue = new FloodCharacterValue();             7             floodCharacterValue = FloodCharacterValue.transFldPropToFloodCharacterValue(editCharaFldprop); 8             m_doc.m_floodExtractCharacterValue = floodCharacterValue; 9         }10     }
3. Add an adapter and you can also monitor it.

First, let's talk about the differences between the adapter and the listening interface: A new listening interface needs to reload all methods under this interface. If you delete any method, an error is returned; however, the adapter can only reload the method you need.

Take windowListener as an example. Add the listener addWindowListerner to the dialog box dialog, and set the listener content to the window adapter WindowAdapter. Comparison:

(1) Listener interface [all methods are reloaded by default. This function is not recommended for a single time]
                                        dialog.addWindowListener(new WindowListener() {                                            @Override                                            public void windowOpened(WindowEvent e) {                                                // TODO Auto-generated method stub                                            }                                            @Override                                            public void windowIconified(WindowEvent e) {                                                // TODO Auto-generated method stub                                            }                                            @Override                                            public void windowDeiconified(WindowEvent e) {                                                // TODO Auto-generated method stub                                            }                                            @Override                                            public void windowDeactivated(WindowEvent e) {                                                // TODO Auto-generated method stub                                            }                                            @Override                                            public void windowClosing(WindowEvent e) {                                                // TODO Auto-generated method stub                                            }                                            @Override                                            public void windowClosed(WindowEvent e) {                                                // TODO Auto-generated method stub                                            }                                            @Override                                            public void windowActivated(WindowEvent e) {                                                // TODO Auto-generated method stub                                            }                                        });
(2) Add an adapter. You can only reload the required method. This function is recommended for one time]
 1     dialog.addWindowListener(new WindowAdapter() { 2         @Override 3         public void windowClosed(WindowEvent e) { 4             // TODO Auto-generated method stub 5             JDialog dialog2 = (JDialog) e.getSource(); 6             set_infoSectionList(update_infoSectionListByProp(prop)); 7             if (basinCheckList != null && !basinCheckList.isEmpty() && basinCheckList.get(0).getCHECKED()==1) { 8                 tableCharaShowPanel.updateData(schemeType, get_infoSectionList(), false);             9             }else {10                 tableCharaShowPanel.updateData(schemeType, get_infoSectionList(), true);            11             }12             super.windowClosed(e);13             dialog2.dispose();14         }15     });

In addition, you can select the adapters to be added, right-click the adapters, and select-resources-override/implement methods...-select the method to be added.

Iv. Summary

The above is an introduction to Java event listening and usage at the entry level. Advanced applications will be learned and recorded later.

 

Written in January 6, 2017

 

Related Article

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.