Events
An event is a user's action on the interface (usually using a variety of input devices, such as mouse, keyboard, etc.).
When an event occurs, the event is represented by an event object. The event object has a corresponding event class. Different event classes describe different types of user actions. The event classes are contained in the java.awt.event and javax.swing.event packages.
Event Source
The component that produces the event is called the event source. When you click on a button, the button is the event source, which produces an actionevent type of event.
Event handlers (event handling methods)
An event handler is a method that receives an event object and processes it accordingly. The event handler is contained within a class, and the object of this class is responsible for checking whether the event occurs and activating the event handler for processing if it occurs.
Event Listener class
Contains the event handler, and is responsible for checking whether the event occurred, and if so, the class that activates the event handler for processing is called the event listener class. In fact, an event listener object. The event listener class must implement an event listener interface or inherit the event listener adapter class.
The event listener interface defines the methods that must be implemented to handle events. The event listener Adapter class is a simple implementation of the event listener interface. The goal is to reduce the amount of programming effort.
The event listener interface and event listener adapter classes are also included in the Java.awt.event and javax.swing.event packages.
registering event listeners
To enable the event listener to check for certain events in a component (event source), and to activate the event handler for appropriate processing when it occurs, the event listener must be registered on the event source. This is done by using the following methods of the event Source component:
Addxxxlistener (Event listener object) xxx corresponds to the corresponding event class.
Example:
Package Test1;import Java.awt.*;import java.awt.event.*;import javax.swing.*;import javax.swing.border.EmptyBorder; public class Simpleactionevent extends JFrame {private JPanel _contentpane;private JButton _JB;//action Events Private JTextField _jt;//Focus Event/** * Launch the application. */public static void Main (string[] args) {Eventqueue.invokelater (new Runnable () {public void run () {try {Simpleactioneven T frame = new simpleactionevent (); frame.setvisible (true);} catch (Exception e) {e.printstacktrace ();}}});} /** * Create the frame. */public simpleactionevent () {setdefaultcloseoperation (jframe.exit_on_close); setbounds (100, 100, 450, 300); _ ContentPane = new JPanel () _JB = new JButton ("I am the button, click Me"); _jt = new JTextField ("Click elsewhere", 10); _contentpane.setborder (New Emptyborder (5, 5, 5, 5)); _contentpane.setlayout (new FlowLayout ());//Action Event _ Jb.addactionlistener (new ActionListener () {@Overridepublic void actionperformed (ActionEvent e) {//TODO auto-generated method stub _ Jb.settext ("I clicked");}); /Focus Event _jt.addfocuslistener (nEW Focuslistener () {@Overridepublic void Focuslost (FocusEvent e) {//TODO auto-generated method stub joptionpane.showmessagedialog (null, "Lose Focus");} @Overridepublic void focusgained (FocusEvent e) {//TODO auto-generated method stub}}); _contentpane.add (_JB); _contentpane.add (_JT); Setcontentpane (_contentpane);}}
Java Event Listener