Java: What's the difference between ActionEvent and ItemEvent on a JRadioButton?

狂风中的少年 提交于 2019-12-01 03:02:42

问题


They're both raised once after the mouse button is released and both can have all the information available on the JRadioButton right? Is there any difference?


回答1:


An ItemListeners are notified when ever the state of the button is changed, whether through a user interacting with the button or programmatically (via the setSelected method). ActionListeners on the other hand will be called when a user interacts with the button (but can be simulated programmatically via the onClick method).

Note that a user interacting with the button such as clicking or hitting the space bar will also change the state of the button and raise an item event as well as an action event. Generally, you will want to define either one or the other, don't listen for both action events and item events on the button.




回答2:


ItemEvent is specific event that indicates that the state has changed in a component specified by the itemStateChanged in the ItemListener. JRadioButton has two states on and off. ItemEvent is also useful when a radio button is part of a button group.

I think you should use the item listener when you are interested in state changes in the radio button and an action listener when you want to do something when the radio button is clicked.



来源:https://stackoverflow.com/questions/3499176/java-whats-the-difference-between-actionevent-and-itemevent-on-a-jradiobutton

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!