Additional information added to JMenuItem
-
24-09-2019 - |
Question
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
Solution
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
OTHER TIPS
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.