如何对JComeboBox中的选项加监听?

waitforeverything 2009-08-02 03:39:33
如何对JComeboBox中的选项加监听? 在JComeboBox中添加了一些图片,想每次选这些图片的时候返回一个参数,方便我写入数据库,该怎么办?
...全文
139 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
KingZChina 2009-08-03
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 jidushanbojue 的回复:]
加if判断一下试试
Java code
comboBox.addItemListener(new ItemListener() {publicvoid itemStateChanged(ItemEvent arg0) {// TODO Auto-generated method stubif (arg0.getStateChange()== ItemEvent.SELECTED) {
System.out.println(jc.getSelectedItem().toString());
System.out.println(jc.getSelectedIndex());
}

}
});
[/Quote]

顶 正解啊!
jidushanbojue 2009-08-03
  • 打赏
  • 举报
回复
加if判断一下试试

comboBox.addItemListener(new ItemListener() {

public void itemStateChanged(ItemEvent arg0) {
// TODO Auto-generated method stub

if (arg0.getStateChange() == ItemEvent.SELECTED) {
System.out.println(jc.getSelectedItem().toString());
System.out.println(jc.getSelectedIndex());
}

}
});


KingZChina 2009-08-03
  • 打赏
  • 举报
回复
接口 ItemListener

方法 itemStateChanged(ItemEvent e) 在用户已选定或取消选定某项时调用。
因为你选择其他的 出现了 取消选定 和选定其他 所以调用了2次
waitforeverything 2009-08-03
  • 打赏
  • 举报
回复
jc = new JComboBox(image);
jc.addItemListener(new ItemListener() {

public void itemStateChanged(ItemEvent e) {
System.out.println(jc.getSelectedItem().toString());
System.out.println(jc.getSelectedIndex());
我加了这个监听 为什么输出的时候输出两便?不知道是怎么回事?
xiaozejun 2009-08-02
  • 打赏
  • 举报
回复
对JComeboBox加一个触发事件 或者加一个有监听事件的控件 把这个控件隐藏并和JcomeBox绑定呢

62,621

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧