开发者

How to code which CheckBoxMenuItem is selected in Java?

开发者 https://www.devze.com 2022-12-25 02:24 出处:网络
I am trying to add a method which take开发者_如何学运维s the menu selection and passes it to a variable.

I am trying to add a method which take开发者_如何学运维s the menu selection and passes it to a variable. In this case I would like to take "7" and insert that into the year variable if that is what is selected from teh checkboxmenu. So far have search the internet and sun site with no clear example on how to differentiate the menu items. Any suggestions would be appreciated thanks.

cbMenuItem = new JCheckBoxMenuItem("7 year");
cbMenuItem.setMnemonic(KeyEvent.VK_H);
cbMenuItem.addItemListener(this);
menu.add(cbMenuItem);

cbMenuItem = new JCheckBoxMenuItem("15 year");
cbMenuItem.setMnemonic(KeyEvent.VK_S);
cbMenuItem.addItemListener(this);
menu.add(cbMenuItem);


menuItem.setActionCommand("7");

Then in your ItemListener you can use the getActionCommand(...) method on the selected item.

0

精彩评论

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

关注公众号