0

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?

mKorbel
  • 109,525
  • 20
  • 134
  • 319
shree
  • 2,745
  • 7
  • 28
  • 35
  • I think you're logics backwards (at least from the perspective of the code example), I think it should be `btn.addActionListener(this);` – MadProgrammer Aug 28 '12 at 05:23
  • 1
    Please try to write in proper English and do not use all those abbreviations... – home Aug 28 '12 at 05:23
  • It's all about callback functions: http://en.wikipedia.org/wiki/Callback_(computer_programming) – home Aug 28 '12 at 05:24

4 Answers4

3

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.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
2

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.

npinti
  • 51,780
  • 5
  • 72
  • 96
2

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...

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
1

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

user207421
  • 305,947
  • 44
  • 307
  • 483
  • I think not only in swing ..most of the all in-built interfaces and abstract classes in java are working in same concept only...and Can we create interface same as it is?how? – shree Aug 28 '12 at 05:28
  • @shree 1. Are you asking or telling here? 2. If you're asking whether you can create an interface, of course you can, but you also have to organize somebody to call it. It doesn't just happen by magic. – user207421 Aug 28 '12 at 07:13