0

I understand the delegation event model in Java, is that it consist of a source that generates an event and send it to one/more listeners... But my problem is; what is the meaning of "generates event", I read books on Java but I see that they don't talk about it. Is that creating an event ( instance ) and throwing it( like exceptions) ? or using flags things.. so I talk about what something that is hided. so let's take an example a button; is a source event ( generates the ActionEvent), ok but how ..?(generates is ... ? I hope the question is clear.. waiting for your comments/answers.

Mike Zboray
  • 39,828
  • 3
  • 90
  • 122

2 Answers2

1

Generating an event is just creating an event object, and calling the listeners. For example, when a button is clicked, it does something like the following:

ActionEvent event = new ActionEvent(this, ...);
for (ActionListener listener : registeredActionListeners) {
    listener.actionPerformed(event);
}
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • thank you, thats what I needed to know, but I still have a question; is that how It(button) knows that the it was clicked ...? –  Jul 02 '14 at 16:44
  • @zarir I recommend you reading [the docs](http://docs.oracle.com/javase/tutorial/uiswing/events/actionlistener.html) and also try using [Google like this](https://www.google.com.mx/search?client=ubuntu&channel=fs&q=jbutton+addactionlistener&ie=utf-8&oe=utf-8&gfe_rd=cr&ei=6Tm0U4ulMIeE8QfUlYGoCg#channel=fs&q=jbutton+actionlistener) so you can get some results. Also try reading [this post](http://stackoverflow.com/questions/284899/how-do-you-add-an-actionlistener-onto-a-jbutton-in-java). Good Luck :) – Frakcool Jul 02 '14 at 16:58
0

I suggest reading about the Observer Pattern to see how a 'Subject' (eg a button) notifies its 'Observers' (eg event listeners)

rob
  • 1,286
  • 11
  • 12