This is the 12th day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021

Hello! Hello, everyone! I’m Grey Ape!

Today I want to summarize the basic functions and functions of the focus event listener function in Java GUI programming.

We know in the Java form of the component, itself is not have any function, so we must add to the control of the corresponding event listener, in order to trigger the corresponding event processing in the interface, today to share with you the use of the focus event listener in Java.

Introduction to Listening Events

First of all, what listening event?

In swing event model, event processing is completed by three separate objects, namely event source, event and listener. The event source triggers an event, which is listened by one or more listeners, and the listener triggers the event and executes the corresponding event.

An event listener, on the other hand, is an “implement specific listener interface” class object. An event is almost always represented as an object. It is an object of an event class, and the event source (a control, such as a button) generates an event object when the user takes the corresponding action (such as clicking a button).

Note that all event sources have the addXXXListener() and removeXXXListener() methods, where XXX indicates the type of listener to be added. The former means to add the corresponding listener and the latter means to remove the corresponding listener.

Two common types of event listeners in Java are “action event listeners” and “focus event listeners”. In the last article I introduced you to the use of action event listeners, so in this article we will take a look at the use of focus event listeners.

Focus event listeners

FocusEvent listeners are as widely used in practice as action event listeners. For example, FocusEvent listeners can be used to trigger an event response when the cursor moves away from an event source, or to return focus to the event source. Here are the interfaces to focus event listeners and common event sources:

The name of the event

The event source

Listening to the interface

Methods to add or remove listeners of the corresponding type

FocusEvent

Component and derived classes

FocusListener

AddFocusListener (), removeFocusListener ()

Here is an example to illustrate what the event listener should do. When the first text box loses focus, the event is triggered to perform a pop-up prompt, with the following code:

package focusEvent; import java.awt.Container; import java.awt.Font; import java.awt.event.FocusListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JTextArea; import javax.swing.WindowConstants; public class FocusEvent extends JFrame{ JTextArea jTextArea1; JTextArea jTextArea2; Public FocusEvent() {// TODO auto-generated constructor stub setTitle(" Action Event listener "); SetSize (400400); Container container = getContentPane(); JTextArea1 = new JTextArea(); JTextArea1. SetBounds (100100200, 30); Jtextarea1.settext (" first step "); Jtextarea1.setfont (new Font(" Regular ",Font.PLAIN,20)); container.add(jTextArea1); jTextArea1.addFocusListener(new JTextArea1Focus()); // create a second text box jTextArea2 = new JTextArea(); JTextArea2. SetBounds (100150200, 30); Jtextarea2.settext (" second step "); Jtextarea2.setfont (new Font(" Regular ",Font.PLAIN,20)); container.add(jTextArea2); setLayout(null); // Empty the form layout manager setLocationRelativeTo(null); // Set the form to center display setVisible(true); SetDefaultCloseOperation (WindowConstants.EXIT_ON_CLOSE); // Center the form setDefaultCloseOperation(WindowConstants. } public static void main(String[] args) {new FocusEvent(); } // Create an inner class, Class JTextArea1Focus implements FocusListener{@override // Override the focus method public void FocusGained (Java awt. Event. FocusEvent arg0) {} @ Override / / rewrite loses focus method public void focusLost (Java. The awt. Event. FocusEvent Arg0) {jTextarea1. setText(" Please finish first!" ); JOptionPane. ShowMessageDialog (null, "please finish the first step!" , "tip", JOptionPane. INFORMATION_MESSAGE); }}}Copy the code

The effect is as follows:

When using focus event listeners, note the following:

  1. Using this listener requires implementing the FocusListener interface

  2. The focusLost() method overridden in this interface is called when the component loses focus.

  3. The overridden focusGained() method in this interface is called when the component gains focus.

  4. In general, listening events for event sources should be in the form of anonymous inner classes.

Think useful remember to like attention yo!

Big bad Wolf is looking forward to progress with you!