开发者

Additional information added to JMenuItem

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


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜