How does the 'actionPerformed' method get called without an explicit call to it?

后端 未结 2 1657
暗喜
暗喜 2021-02-19 22:11

I just started learning GUI with Swing and don\'t exactly understand how the actionPerformed method works. Consider the following code:

//code to cr         


        
相关标签:
2条回答
  • 2021-02-19 22:49

    But there is a specific call to this method, only it's not occurring in your code but rather in the JVM. A button push instigates internal events that leads the JVM to tell the button to notify all of its listeners that it has been pushed. This will cause the actionPerformed methods of all attached ActionListeners to be called.

    To see information on how this works, first look at the Java API for the AbstractButton class where you'll find the method

    protected void fireActionPerformed(ActionEvent event)
    

    Where

    Notifies all listeners that have registered interest for notification on this event type. The event instance is lazily created using the event parameter.

    Then for further information, you will want to go beyond the Java API to the source code which can be found here. If you check out the Java 8.0 source code there, and look up javax then swing, then AbstractButton, you'll find a fireActionPerformed(ActionEvent event) method:

    2002    protected void More ...fireActionPerformed(ActionEvent event) {
    2003        // Guaranteed to return a non-null array
    2004        Object[] listeners = listenerList.getListenerList();
    2005        ActionEvent e = null;
    2006        // Process the listeners last to first, notifying
    2007        // those that are interested in this event
    2008        for (int i = listeners.length-2; i>=0; i-=2) {
    2009            if (listeners[i]==ActionListener.class) {
    2010                // Lazily create the event:
    2011                if (e == null) {
    2012                      String actionCommand = event.getActionCommand();
    2013                      if(actionCommand == null) {
    2014                         actionCommand = getActionCommand();
    2015                      }
    2016                      e = new ActionEvent(AbstractButton.this,
    2017                                          ActionEvent.ACTION_PERFORMED,
    2018                                          actionCommand,
    2019                                          event.getWhen(),
    2020                                          event.getModifiers());
    2021                }
    2022                ((ActionListener)listeners[i+1]).actionPerformed(e);
    2023            }
    2024        }
    2025    }
    
    0 讨论(0)
  • 2021-02-19 23:08

    Each event is represented by an object that gives information about the event and identifies the event source. Event sources are often components or models, but other kinds of objects can also be event sources.

    Here,the listener you registered, that is,

    button.addActionListener(this);
    

    gets added to a list of listeners, and when the JVM receives an event (click in this case), it calls the appropriate methods on all the listeners in the list.

    How does this happen? Well, i think you should read about Callback mechanism in java.

    You can also create your own listeners using Callback mechanism. Consider the below code:

    The code is for a credit-card app simulation.In the below code, the pinChanged() method gets called automatically when the changePin() method is called.

    public interface PinChangeListener {
        public void pinChanged();
    }
    
    public class CreditCard {
        public PinChangeListener pinChangeListener;
    
        private int pin;
    
        public changePin(int pin) {
            this.pin = pin;
            if (pinChangeListener != null) {
                pinChangeListener.pinChanged();
            }
        }
    }
    

    To connect a callback/listener to the credit card you just need to implement the PinChangeListener method:

    creditCard.pinChangeListener = new PinChangeListener() {
        public void pinChanged() {
            System.out.println("The pin has been changed");
        }
    };
    

    Similarly, when you attach a listener to a button, the click is detected by JVM,(You probably don't want to go into how the click is detected!) and the actionPerformed() of that listener which is attached is called by the JVM for you. Hope this clears.

    0 讨论(0)
提交回复
热议问题