Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Highlighted
Developer
Posts: 98
Registered: ‎12-29-2009
My Device: Bold
My Carrier: Rogers
Accepted Solution

Call a button "invokeAction" from Menu item

Hi,

 

If I have a button and have written some code for it's invokeAction method, is it possible to call that button's invokeAction method from a menu item?

 

I suppose I could just copy the code for this button and add it to the menu; I'm really just wondering if there's an easier way.

 

(Sorry if this has already been covered in this forum, I'm not quite sure of the kinds of search terms I would use to find a solution to my question.)

 

Thanks!

 

Developer
Posts: 94
Registered: ‎10-01-2009
My Device: Not Specified

Re: Call a button "invokeAction" from Menu item

invokeAction is protected, so you can't call it directly. You can play games like deriving a subclass with invokeAction made public. But it's usually simpler to just have the button's listener and the menu item's listener invoke a common method. That's easy and obvious if the listeners are in the same code module and a little more complicated if they're not, of course.  But, in general duplicate code is a Bad Idea, and it's even worse if the duplicates are in different modules.

Developer
Posts: 98
Registered: ‎12-29-2009
My Device: Bold
My Carrier: Rogers

Re: Call a button "invokeAction" from Menu item

Gosh--it makes total sense to have the "button's listener and the menu item's listener invoke a common method."

 

Thanks jMarc!