Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

java adapter pattern  RSS feed

 
babak azarmi
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
before i start my question let me show or better say remind you event handling in javaSE (swing) for example i want to handle mouseClicked event.

JButton has a method that name is addMouseListener(MouseListener m);

MouseListener is an interface that has more than 4 mehtods and we want to override the mouseClicked(MoueEvent e) method.

my main question is in addMouseListener when we Override the mouseClicked
the addMouseListner method how it can specify or understand the mouseClicked method is ovrriden and not mouseMoved or the others method.
the client overrid the mouseClicked for himself. and addMouseListner run the overrid method for him.!?

How can write my own adapter? please write me an example.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't choose which interface methods you implement - you have to implement them all.
Java does however provide a basic implementation of the MouseListener interface in the MouseAdapter class which just provides empty implementations of each of the MouseListener methods. Your class can extend the MouseAdapter class and just override the mouseClicked method.
 
babak azarmi
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i dont want to use java api, i want to have my business interface with it's class addapter and i override their methods by myself and adapter class chooses the overrid method and run the method?

if i override all the methods , it's not good solution because i know some of them arent useful for my specified business
 
babak azarmi
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this is my sample code








now just assume that in my MyListener Interface i have 2 or more methods
now how adaptee chooses which method is override
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If your interface has more than one method then any non-abstract implementing class has to implement all the methods - that's the way Java works.

Your implementation of the addListener method is a little unusual. Normally, all the method will do is add the class instance that was passed as a parameter to a list of some sort. Then whenever whatever event you are listening for actually happens, it will call the specified method on all the objects in the list.
[ November 07, 2008: Message edited by: Joanne Neal ]
 
Campbell Ritchie
Sheriff
Posts: 53773
127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by babak azarmi:
i dont want to use java api . . .


Yes, you do.

The Components specifically take a Listener which is specified as java.awt.event.FooListener. If you want to add your own class it must implement those interfaces (or inherit from them), so you still have to implement all the methods in the interface. MouseListener has 5 methods (not 4, if I remember correctly) so you have to implement 5 methods. You can either implement all 5 in which case some will have /* empty */ as their method bodies, or use the MouseAdapter class with the @Override annotation to make sure you have spelled the method names correctly.
 
babak azarmi
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your implementation of the addListener method is a little unusual. Normally, all the method will do is add the class instance that was passed as a parameter to a list of some sort. Then whenever whatever event you are listening for actually happens, it will call the specified method on all the objects in the list.


thank you so much

would you complete my code and show me an example and implement my addListener method usual. and also change your quote to code for me?

for example i changed my









you see, in Main class i implemented another method but still i don know how tell addListner method that choose my desired method. please fill the addListner method for me
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Now, whenever an instance of the MyAdaptee class wants to tell the listeners that an event has happened, it needs to iterate thru the listeners list and call the appropriate method (test or testPrime) on each object depending on what sort of event has happened
[ November 07, 2008: Message edited by: Joanne Neal ]
 
Rob Spoor
Sheriff
Posts: 20895
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by babak azarmi:


Please, don't throw exceptions. Just do nothing. That's how all adapter classes do it.


While usually we don't provide implementations just like that (Do Your Own Homework and Not A Code Mill), the following can be found in the source of the Java API.

There should be one fireXXX method for each event that you can fire (a.k.a. each method of the listener). Usually these methods take parameters which will help you build an event for the listener as follows:

The YYY can be an identifier for the type of event. Check out some of the subclasses of java.util.EventObject for more information.

Your listener doesn't have any events so that's not necessary here.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And just to put it together, If we look at the interface and the adapter, then use a simple demo main class we can see what happens:


These listeners and adapter would somehow work together with the MyEventSource, which in a very simplified fashion just takes listeners and fired the appropriate event:
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!