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

mouse event redispaching  RSS feed

 
Jigar Gosar
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
my problem is simple but the solution might not be,
description:
i have a jpanel(inside a JFrame) which is large(300,300) , and has a mouse listner added to it, it contains a label, which also has a mouselistener added to it.
now, what i want is whenever i click on the label its mouse pressedevent gets called, inside that method it is determined that it is not fesable to handle this event(this happens sometimes), so i want the jpanel(labels parent) to handle the event, now what code should i write in jlabels mousepressed method so that the event is redispached to jpanel, and mousepressed of jpanel, gets called. remember this decision of whether to redispatch the event to parent is taken in jlabels mousepressed method,
please suggest a solution,
actually i am building a logic circuit simulator, and am stuck at a point, letting me know so as how to redispatch events will solve my problem,
please help,
regards,
Jigar Gosar.
 
Jigar Gosar
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if nobody has an answer to this question, then please can someone move this post to java intermediate, or advance forum.
thanks.
 
saager mhatre
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ouch! Aren't we a little touchy!
Your problem requires some study of the Delegation Event Model and the handling of 'consumed' and 'unconsumed' events. I just need some time to look into the specifics, but this should give you some direction. Basically, if your JLabel doesn't 'consume' the MouseEvent, it's supposed to go back up the Component hierarchy. That's when your JPanel should be able to catch it.
I'll try to get back with more details soon.
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"cujigar" -

Welcome to the JavaRanch! Please adjust your displayed name to meet the
JavaRanch Naming Policy.
You can change it here.

A simple way to do this is just to get the parent component and dispatch the event to it... here's some code illustrating this.



Thanks! and welcome to the JavaRanch!
 
Jigar Gosar
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for answer,(i think my problem has nothing to do with consume
but my problem is little more complicated.
imagine a situation, where
there is a panel A, inside it we have panel B, inside it we have label L.
now only A & L are registered for mouse events(and not B), now inside L's mouse pressed event, i want to redispatch the event to the very first parent up the hierarchy(in this case A) who has a listener registered, now how is this possible..
and i need a genric answer, where the components, may be nested n levels deep, but the "next up the hierarchy listner" is to be invoked, if u can answer this problem, i will be highly greatful..
one more thing,
in he answer suggested........

what if parent doesnot have a listener registered, will the event propogate up the hierarchy, where parent's parent has some listener registered, by the above call.
i am sure bartender has the answer,
so please help me.
thanks,
regards,
Jigar Gosar
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, if no listeners are attached to the parent, then nothing happens. There are two things I see that you could do. You could write a subclass of panel that you use inside the top level JPanel that contains all the components you are interested in, which has a default listener that just does a "pass event to parent". You could also have a static reference somewhere to get the top level panel of your app directly, then, instead of "pass event to parent" you would do a "pass event to top panel" through the static reference. A static reference should be fine in this case, since there should only be one top level panel in your application (as I understand it).
 
Jigar Gosar
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
that was all that i need...
thanks a lot......
the idea of static reference was very helpful...
but i did not use static, but a non-static reference.....
problem solved.....
thanks again,
regards,
Jigar Gosar.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!