Hi all,
Me again. I am working on menus right now. Menus in Java seems rather nice
and simple, but there is one thing I do not figure out : in my action
listener, how can I detect which menu option has been chosen? Under Windows,
it is simple as you have a menu identifier and you can check it. But I see
no such thing under Java and Swing. Should I keep a pointer to every
menuitem I have created and compare it to the object returned by getSource
in the action listener? Should I derive the JMenuItem class and add a menu
identifier of my own to manage this? Or is it a simpler way of proceeding?
How do you do this in your programs?
Thanks, Francois
visionset - 01 May 2007 19:33 GMT
> Hi all,
>
[quoted text clipped - 7 lines]
> add a menu identifier of my own to manage this? Or is it a simpler way of
> proceeding? How do you do this in your programs?
Generally speaking, either add a separate action listener to each item or
use the ActionEvent.getActionCommand() method to identify.
Usually you can use both with the latter for closely related actions.
Also, consider using the Command pattern implemented by the Action class.

Signature
Mike W