If you have read any type of of the component how-to pages, you most likely already recognize the basics of occasion listeners.
You are watching: Which of the following true about source and listener objects?
Let us look at among the simplest occasion handling examples possible. It is called Beeper, and also it functions a button that beeps when you click it.
Click the Launch switch to run Beeper using Java™ Internet Start (downfill JDK 7 or later). Conversely, to compile and also run the example yourself, consult the example index.
You deserve to uncover the entire regimen in Beeper.java. Here is the code that implements the event managing for the button:
public class Beeper ... implements ActionListener ... //wright here initialization occurs: button.addActionListener(this); ... public void actionPerformed(ActionEvent e) ...//Make a beep sound...
The Beeper course implements the ActionListener interchallenge, which includes one method: actionPercreated. Since Beeper implements ActionListener, a Beeper object can register as a listener for the activity occasions that buttons fire. Once the Beeper has been registered using the Button addActionListener approach, the Beeper's actionPercreated approach is called eexceptionally time the button is clicked.
A More Complex Example
The event version, which you witnessed at its easiest in the preceding example, is fairly powerful and functional. Any variety of occasion listener objects deserve to listen for all kinds of occasions from any variety of event source objects. For example, a regimen could produce one listener per occasion resource. Or a program could have a solitary listener for all events from all resources. A program can even have more than one listener for a solitary kind of occasion from a solitary occasion resource.
Multiple listeners can register to be notified of occasions of a specific type from a specific resource. Also, the exact same listener deserve to listen to notifications from different objects.
Each occasion is represented by an object that provides information around the occasion and identifies the occasion resource. Event sources are frequently components or models, yet other kinds of objects have the right to additionally be occasion resources.
Whenever you desire to detect occasions from a specific component, first check the how-to area for that component. A list of the component how-to sections is right here. The how-to sections offer examples of handling the events that you are many most likely to care around. In How to Use Color Choosers, for circumstances, you will uncover an example of composing a readjust listener to track as soon as the color changes in the shade chooser.
The adhering to instance demonstrates that occasion listeners have the right to be registered on multiple objects and also that the same event deserve to be sent out to multiple listeners. The example has 2 occasion resources (JButton instances) and two event listeners. One of the occasion listeners (an circumstances of a course dubbed MultiListener) listens for events from both butlots. When it receives an occasion, it adds the event's "activity command" (which is collection to the message on the button's label) to the peak message area. The second occasion listener (an instance of a course called Eavesdropper) lis10s for occasions on only among the butloads. When it receives an event, it adds the activity command also to the bottom text area.
Try this:Click the Blah blah blah switch. Only the MultiListener object is registered to listen to this button.Click the You perform not say! button. Both the MultiListener object and also the Eavesdropper object are registered to listen to this button.
See more: Why Does Crazy Frog Have A Penis Cleared By Asa, Crazy Frog And His Stunted Penis Cleared By Asa
You deserve to discover the whole regimen in MultiListener.java. Here is the code that implements the occasion managing for the button:
public course MultiListener ... implements ActionListener ... //wbelow initialization occurs: button1.addActionListener(this); button2.addActionListener(this); button2.addActionListener(brand-new Eavesdropper(bottomTextArea)); public void actionPerformed(ActionEvent e) topTextArea.append(e.getActionCommand() + newline); }class Eavesdropper implements ActionListener ... public void actionPerformed(ActionEvent e) myTextArea.append(e.getActionCommand() + newline);
In the above code, both MultiListener and Eavesdropper implement the ActionListener interface and register as action listeners utilizing the JButton addActionListener method. Both classes' implementations of the actionPercreated technique are similar: they ssuggest add the event's activity command to a text location.