简体   繁体   中英

How interface method and abstract method calling default?

When we implement Listener, Renderer or Editor, inside methods how Java its calling automatically?

Code:

Class A implements ActionListener{
   A(){
    //bla bla
    //bla bla 
    this.addActionListener(btn);

   }
   public void actionPerformed(ActionEvent e){**// How actionPerformed method called        //automatically if we register button**

   }
}

How its calling actionPerformed method automatically after registering button object? We are just passing the btn object into addActionListener(btn) . How inside its calling that method?

I checked through netbeans inside addActionListener method*. There is no calling method of actionPerformed method . Also if we register it keeps on working. Is it calling by thread anywhere inside ? But i checked source code. nothing is there . How?

Events are dispatched from an EventListenerList , owned by the parent JComponent , using a convention outlined in the API and discussed here . Editors and Renderers are evoked by the owning view component.

Addendum: Can we create interface same as it is? How?

Yes, JFreeChart is a fairly accessible example. Although a chart is not itself a JComponent , it uses the same model for its own events .

In Java, anything which happens upon any windows component is dealt with by the Event Dispatcher Thread :

The event dispatching thread (EDT) is a background thread used in Java to process events from the Abstract Window Toolkit (AWT) graphical user interface event queue. These events are primarily update events that cause user interface components to redraw themselves, or input events from input devices such as the mouse or keyboard.

Whenever you click or do some event, it is the EDT which kick starts the action listener, which is why doing any Thread.sleep in your action listener will eventually freeze the UI for a period of time.

Since your class implements a given interface, your class will guarantee the EDT that it will have a series of methods which the EDT can use to do whatever it needs.

For more information on the EDT, please take a look at this Oracle document.

It's magic.

Event handling is taken care of for you by the AWT API. These events are then queue and dispatched to the various components (via a serious of steps). Each interested party then handles those requests that are of interest to them before passing them up the food chain till it reaches you.

The question is, should you care?

In some respects yes, but do you care how electricity works or just that you can turn on the light switch?

I'm sure there's better documentation, but you could take a look at http://docs.oracle.com/javase/1.3/docs/guide/awt/designspec/events.html for starters...

Swing calls your ActionListener automatically when the action occurs. The actual method call is located deep inside the source code of Swing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM