views:

35

answers:

2

Hello,

I'm developing a java application and I create a dynamic JMenu by hand (depending on the input data). I add the same action for each menu item of the menu. The problem is that depending on the menu item called I would like to take a different action each time. Is there a simple way of doing this (Something like setData(Object data) for each JMenuItem or am I forced to derive from JMenuItem)?

Thanks in advance,

Iulian

+4  A: 

i guess you can use ActionEvent.getSource in the actionPerformed method of your Action. compare the source of the event with your JMenuItem's.

to store additional info, you can use putClientProperty or extend JMenuItem

Stefan De Boey
Yes, that gives me the object. But I want additional information, not only the object. Is there a way for doing this?
Iulian Şerbănoiu
what additional info do you need? once you know which of the item is the source, you can do something different for each item
Stefan De Boey
What kind of information, complementary of the source object, would you need ?
Riduidel
Another object, beside the JMenuItem
Iulian Şerbănoiu
i understand, the menu is dynamic (missed that part), i've updated my answer
Stefan De Boey
+1  A: 

According to initial question and remarks, I would suggest Julian to replace its JMenuItem built with a string and an associated action with a JMenuItem built with only an action, providing both the text and the associated behaviour. This action could reference others elements from application to provide its requireds behaviour.

Riduidel
I could have this way tons of actions. I will go with putClientProperty.
Iulian Şerbănoiu