JComboBox事件响应

写在最前

最近一段时间都十分忙,一直没有时间更新这边的文章,好在清明小假期终于能有一段调整的时间了,今日继续。

摘要

对于日常Swing开发,JComboBox添加事件响应是十分常见的工作内容之一。一般来说,常用的手段有addItemListener及addActionListener。那么两者异同在在什么地方?何时该用ItemListener,而又何时该用ActionListener呢?

适用场景

1. 当JComboBox选择内容发生变化时ItemListener会响应2次,而ActionListener会响应1次;
2. 当JComboBox选择内容未发生变化时ItemListener会响应0次,而ActionListener会响应1次;

示例代码:

    JComboBox<String> combox = new JComboBox<String>();
    combox.addItem("AAA");
    combox.addItem("BBB");
    combox.addItem("CCC");

    combox.addItemListener(e -> System.out.println("itemStateChanged"));
    combox.addActionListener(e -> System.out.println("actionPerformed"));

现象:

当第一次选择“BBB”时,输出为:

    itemStateChanged
    itemStateChanged
    actionPerformed

当再一次选择“BBB”时,输出为:

    actionPerformed

进一步分析

替换实例代码如下:

    combox.addItemListener(e -> System.out.println(e));
    combox.addActionListener(e -> System.out.println(e));

重复上述操作,当第一次选择“BBB”时,输出为:

    java.awt.event.ItemEvent[ITEM_STATE_CHANGED,item=AAA,stateChange=DESELECTED] on javax.swing.JComboBox[,0,0,196x74,layout=com.apple.laf.AquaComboBoxUI$AquaComboBoxLayoutManager,alignmentX=0.0,alignmentY=0.0,border=,flags=16777536,maximumSize=,minimumSize=,preferredSize=,isEditable=false,lightWeightPopupEnabled=true,maximumRowCount=8,selectedItemReminder=AAA]
    java.awt.event.ItemEvent[ITEM_STATE_CHANGED,item=BBB,stateChange=SELECTED] on javax.swing.JComboBox[,0,0,196x74,layout=com.apple.laf.AquaComboBoxUI$AquaComboBoxLayoutManager,alignmentX=0.0,alignmentY=0.0,border=,flags=16777536,maximumSize=,minimumSize=,preferredSize=,isEditable=false,lightWeightPopupEnabled=true,maximumRowCount=8,selectedItemReminder=BBB]
    java.awt.event.ActionEvent[ACTION_PERFORMED,cmd=comboBoxChanged,when=1490977820123,modifiers=Button1] on javax.swing.JComboBox[,0,0,196x74,layout=com.apple.laf.AquaComboBoxUI$AquaComboBoxLayoutManager,alignmentX=0.0,alignmentY=0.0,border=,flags=16777536,maximumSize=,minimumSize=,preferredSize=,isEditable=false,lightWeightPopupEnabled=true,maximumRowCount=8,selectedItemReminder=BBB]

当再一次选择“BBB”时,输出为:

    java.awt.event.ActionEvent[ACTION_PERFORMED,cmd=comboBoxChanged,when=1490977820123,modifiers=Button1] on javax.swing.JComboBox[,0,0,196x74,layout=com.apple.laf.AquaComboBoxUI$AquaComboBoxLayoutManager,alignmentX=0.0,alignmentY=0.0,border=,flags=16777536,maximumSize=,minimumSize=,preferredSize=,isEditable=false,lightWeightPopupEnabled=true,maximumRowCount=8,selectedItemReminder=BBB]

看到这里,响应次数区别的原因就一目了然了,之所以当选择项发生变化时ItemListener会响应2次的原因为:
1. 当ItemEvent为DESELECTED及SELECTED时,ItemListener各响应了一次;
2. 当ActionEvent的comboBoxChanged时ActionListener一定会响应一次的。

总结

ItemListener及ActionListener两者的异同如下:

  • 相同处:ItemListener ActionListener都是JCombobox的事件响应Listener;
  • 不同处
    • 选中项改变时ItemListener响应2次,选中项不变时ItemListener响应0次;
    • 选中项改变时ActionListener响应1次,选中项不变时ActionListener响应1次;
    • ItemListener适用于因stateChange不同而进行不同事件响应的情况;
    • ActionListener适用于不论选中项是否发生变化都需要事件响应的情况;
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 国家电网公司企业标准(Q/GDW)- 面向对象的用电信息数据交换协议 - 报批稿:20170802 前言: 排版 ...
    庭说阅读 13,895评论 6 13
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,773评论 19 139
  • 1、窗体 1、常用属性 (1)Name属性:用来获取或设置窗体的名称,在应用程序中可通过Name属性来引用窗体。 ...
    Moment__格调阅读 10,059评论 0 11
  • 第一章 Nginx简介 Nginx是什么 没有听过Nginx?那么一定听过它的“同行”Apache吧!Ngi...
    JokerW阅读 32,893评论 24 1,002
  • 连绵不绝的夏雨终于停了,清晨睡意朦胧中没再听到窸窸窣窣的滴答响。说是醒了,其实还在与梦魇纠缠,昏昏沉沉的,脑子很...
    嘻锦阅读 1,553评论 2 4