• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why Interfaces in Event handling?  RSS feed

 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't understand why actually INTERFACES are used in event handling in java..?
I read somewhere that it provides a bridge between the listener, and the event source..
But, i don't actually understand that line... what does it means?
 
Saloon Keeper
Posts: 7994
143
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let's say you have a piece of software that generates events, and you have a piece of software that needs to know about events and deal with them. Often the software that generates events doesn't know about the existence of the handlers.

Let's say you have a Swing form with a button. When you click the button, it generates an event you would like to handle. However, how were the designers of Swing supposed to know *what* should happen when you click the button? And obviously, different things should happen when the button is used in different programs.

Your code somehow needs to interface with the button. Swing does this by providing ActionListener. This is an interface you can implement (essentially saying what should happen when some event occurs), and then you can add an instance of your class to the button's list of handlers. Even though the button doesn't know how to handle events, it *does* know how to tell ActionListeners to deal with the event. Here's an example:
You could say that ActionListener provides a bridge between ExitAction and JButton.
 
Andy Richard
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
okay, got that. thanks
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!