Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to handle events from multiple sources?  RSS feed

 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I am having some trouble figuring out how to handle events from multiple menu's. For example, say I have the following MenuBar with two Menu's ("One", and "two", and each menu has a menuItem "Hi". How can I tell which menu item is sending it? Thanks in advance.

The menu would look like that. ( the - denotes a menu item)

 
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let them notify different listener instances.
 
Bob Zoloman
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do I do that?
 
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you create menu1, call addActionListner( listener1 ). For menu2 call addActionListener( listener2 ). Each listener has code to do the appropriate thing.

I'm guessing you have one listener and a check for the EventObjet source text equal to "Hi". Sure enough you can't tell the difference between the two menu items. If you keep references to the menuitems in instance variables and compare the EventObject source to those objects you can tell them apart in a single listener

Do either of those approaches work for you? I have some more advanced cards up my sleeve yet if you're interested.
[ September 07, 2006: Message edited by: Stan James ]
 
Bob Zoloman
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the code! I got it to work, but I used a different way. I made some inner classes, one for each menuItem. I think your way saves a lot of typing i'll give it a try, thanks.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Glad you got it going! A class - inner class or not - per menuitem is pretty much what Ilja said and my first example. I actually prefer it to the if/else second example because those can grow out of control and make an over-active site of change.

It's neat to make those inner classes do about one line of work and just pass the news that something happened to another object that does the real job. For example the File/Open item listener shouldn't open a file chooser and get the results and open a file, it should just tell another Plain Old Java Class to doYourOpenFileThingNow(). Then if you later hook up a button or some other widget to do the same thing it can just call the same method.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Bob Zoloman:
I think your way saves a lot of typing i'll give it a try, thanks.


Typing isn't the most dominant force in programming. You will only have to type that code once, but it will be read and modified many times.

And for the latter, I really think the one-class-per-menu-item variant is superior.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!