How To Use Multiple Action Listeners In Java
If you take read whatsoever of the component how-to pages, you probably already know the basics of effect listeners.
Let u.s.a. look at one of the simplest result handling examples possible. It is called Beeper, and it features a button that beeps when you click it.
Click the Launch button to run Beeper using Java™ Web Get-go (download JDK 7 or later). Alternatively, to compile and run the case yourself, consult the case alphabetize.
           
        
 
        You tin notice the entire program in          Beeper.java. Here is the code that implements the event treatment for the button:
public class Beeper ... implements ActionListener {     ...            //where initialization occurs:            push button.addActionListener(this);     ...     public void actionPerformed(ActionEvent e) {            ...//Make a beep audio...            } }                  The          Beeper          class implements the          ActionListener          interface, which contains i method:          actionPerformed. Since          Beeper          implements          ActionListener, a          Beeper          object can register as a listener for the action events that buttons fire. Once the          Beeper          has been registered using the          Button          addActionListener          method, the          Beeper'south          actionPerformed          method is chosen every time the button is clicked.
A More Complex Example
The event model, which you saw at its simplest in the preceding instance, is quite powerful and flexible. Any number of event listener objects tin listen for all kinds of events from any number of event source objects. For example, a program might create one listener per upshot source. Or a program might take a single listener for all events from all sources. A program can fifty-fifty have more than 1 listener for a single kind of event from a single event source.
 
        Multiple listeners tin can register to be notified of events of a particular type from a particular source. Too, the aforementioned listener tin can listen to notifications from different objects.
Each outcome is represented by an object that gives data almost the event and identifies the consequence source. Issue sources are often components or models, but other kinds of objects tin can as well be upshot sources.
Whenever you want to observe events from a detail component, first bank check the how-to section for that component. A list of the component how-to sections is here. The how-to sections give examples of handling the events that you are near likely to care about. In How to Use Color Choosers, for example, you will find an case of writing a modify listener to track when the colour changes in the color chooser.
The following example demonstrates that consequence listeners can be registered on multiple objects and that the aforementioned upshot can exist sent to multiple listeners. The case contains two event sources (JButton          instances) and two event listeners. 1 of the event listeners (an instance of a class chosen          MultiListener) listens for events from both buttons. When it receives an event, information technology adds the event's "action command" (which is prepare to the text on the push's label) to the top text area. The second consequence listener (an instance of a class called          Eavesdropper) listens for events on only one of the buttons. When it receives an event, it adds the activeness command to the bottom text expanse.
 
        Try this:
- Click the Launch button to run MultiListener using  Coffee™ Web Outset (download JDK 7 or later). Alternatively, to compile and run the example yourself, consult the example alphabetize.  
 
- Click the              Blah blah blah              push. Only the              MultiListenerobject is registered to listen to this push.
- Click the              Yous practise not say!              push button. Both the              MultiListenerobject and theEavesdropperobject are registered to heed to this push.
You tin find the unabridged program in          MultiListener.java. Here is the lawmaking that implements the upshot treatment for the push button:
public form MultiListener ... implements ActionListener {     ...            //where initialization occurs:            button1.addActionListener(this);         button2.addActionListener(this);          button2.addActionListener(new Eavesdropper(bottomTextArea));     }      public void actionPerformed(ActionEvent due east) {         topTextArea.suspend(east.getActionCommand() + newline);     } }  class Eavesdropper implements ActionListener {     ...     public void actionPerformed(ActionEvent due east) {         myTextArea.append(e.getActionCommand() + newline);     } }                  In the above code, both          MultiListener          and          Eavesdropper          implement the          ActionListener          interface and register as action listeners using the          JButton          addActionListener          method. Both classes' implementations of the          actionPerformed          method are similar: they simply add the event'south activity control to a text area.
Source: https://docs.oracle.com/javase/tutorial/uiswing/events/intro.html

0 Response to "How To Use Multiple Action Listeners In Java"
Post a Comment