Menus

  • Thread starter Francois Lionet
  • Start date
F

Francois Lionet

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
 
V

visionset

Francois Lionet said:
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?

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,821
Latest member
AleidaSchi

Latest Threads

Top