In this chapter, we will learn in detail one aspect of using swing components: event processing. When using the swing component set, we can use the Delegate-based event processing mechanism, but we can also use other methods to respond to user actions. In this chapter, we will explore all the event handling and response mechanisms. We will also learn how swing manages the input focus and controls the input focus processing technologies.
When exploring the event processing function, we will begin to understand some practical swing components. In this chapter, we will use the swing component in the simplest way. We can first read the components discussed in later sections of this book, and then return to this chapter to discuss event processing. The subsequent sections of this book also contain detailed content for processing specific events of each component.
2.1 delegate-based event handling
Sun introduced a delegate-based event processing mechanism in jdk.1.1 and the Java library of JavaBean. Although the Java 1.0 library contains object observer pairs that follow the observer behavior design pattern, this is not a long-term solution for user interface programming.
2.1.1 event Delegation Model
The delegated event processing mechanism is a special form of observer design patterns. The observer mode can be used when an Observer wants the state of a monitored object to change and what the state changes. In the Delegate-based event processing mechanism, the observer does not listen to changes in the status, but listens to events.
Figure 2-1 shows the modified observer schema structure related to the specific classes of event processing in the Java library. The generic subject in the mode manages a list of generic observer objects for events that subject can generate. The observer object in the list must provide a specific interface through which subject participants can notify them. When an event of interest to the observer object occurs in the subject participant, all registered observer objects will be notified. In the Java World, the Java. util. eventlistener interface must be extended for the interface implemented by the observer object. The Java. util. eventobject class must be extended for events that must be created by subject participants.
To make the discussion clearer, we will learn about the Delegate-based event processing mechanism from the Perspective of Non-design patterns. The GUI component (JavaBean) manages a listener list. each listener has a pair of methods for the listener type: addxxxlistener () and removexxxlistener (). When an event occurs in the component, the component notifies all registered event listeners. Any observer class interested in this event needs to register an implementer for the corresponding interface with the component. When an event occurs, All implementations are notified. Figure 2-2 has tasted this process.
2.1.2 event listener as the observer
You can use Event Listeners to process events in three steps:
- Define a class to implement the corresponding listener interface (this includes implementing all interface methods ).
- Create an instance of this listener.
- Register the listener to the component of the event we are interested in.
Next, let's take a look at these three steps by creating a simple response button that responds to the selection through the Output Message.
Define listener
To set event processing for an optional button, we need to create an actionlistener because jbutton will generate an actionevent object when it is selected.
class AnActionListener implements ActionListener { public void actionPerformed(ActionEvent actionEvent) { System.out.println("I was selected."); } }class AnActionListener implements ActionListener { public void actionPerformed(ActionEvent actionEvent) { System.out.println("I was selected."); }}
Create listener instance
Next, we will create a listener instance.
ActionListener actionListener = new AnActionListener();ActionListener actionListener = new AnActionListener();
If we use anonymous inline classes for event listeners, we can combine steps 1 and 2:
ActionListener actionListener = new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { System.out.println("I was selected."); } };ActionListener actionListener = new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { System.out.println("I was selected."); }};
Register listeners with components
Once we create a listener, we can associate it with the corresponding components. Suppose we have created a jbutton and saved its reference to the variable button. We can call the addactionlistener () method of the button to implement it:
button.addActionListener(actionListener);button.addActionListener(actionListener);
If the currently defined class is the class that implements the listener interface, we do not need to create a separate listener instance. We only need to associate the class as the listener with the component. The sample code is as follows:
public class YourClass implements ActionListener { ... // Other code for your class public void actionPerformed(ActionEvent actionEvent) { System.out.println("I was selected."); } // Code within some method JButton button = new JButton(...); button.addActionListener(this); // More code within some method }public class YourClass implements ActionListener { ... // Other code for your class public void actionPerformed(ActionEvent actionEvent) { System.out.println("I was selected."); } // Code within some method JButton button = new JButton(...); button.addActionListener(this); // More code within some method}
The event processor is the basic method for responding to swing component events. Which listener is used together with which component will be described in the following sections. In the following content, we will learn some other methods to respond to events.