Topic: JComboBox的ActionListener问题

  Print this page

1.JComboBox的ActionListener问题 Copy to clipboard
Posted by: cxp108
Posted on: 2004-07-20 16:08

我在用JComboBox 的时候出现了这个问题:

当我用一个ActionListener监视一个JComboBox时,它不但捕捉到了

用户的操作,同时也把程序中对JComboBox的操作也捕捉到了,这使得我

不得不采取其他办法分辨是用户还是程序在操作这个JComboBox,不知

有什么Listener可以直接解决这个问题.

2.Re:JComboBox的ActionListener问题 [Re: cxp108] Copy to clipboard
Posted by: cxp108
Posted on: 2004-07-20 16:35

急,在线等!

顶!

3.Re:JComboBox的ActionListener问题 [Re: cxp108] Copy to clipboard
Posted by: yamakasy
Posted on: 2004-07-20 23:27

不需要的时候remove掉,需要的时候重新添加listener

4.Re:JComboBox的ActionListener问题 [Re: cxp108] Copy to clipboard
Posted by: cxp108
Posted on: 2004-07-21 09:03

这样动作会不会太大了??

我目前是这样解决的:

JComboBox jcb;
.......
jcb.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(e.getActionCommand().equals("user"){
.....//用户选择时的动作
}
}
})
jcb.setActionCommand("program");
.........//对jcb进行选择操作
jcb.setActionCommand("user");

但是每次都必须设置actioncommand万一出现

遗漏或是在线程中使用就很麻烦.

不知版主是否有什么更好的方法可以一次设置后,

只有用户能够触发.

5.Re:JComboBox的ActionListener问题 [Re: cxp108] Copy to clipboard
Posted by: floater
Posted on: 2004-07-21 09:31

JComboBox is a little bit different than other components, because there are two actions, you shouldn't set the command, instead you should check them, as follows.

if ("comboBoxEdited".equals(evt.getActionCommand()))
else if ("comboBoxChanged".equals(evt.getActionCommand()))

6.Re:JComboBox的ActionListener问题 [Re: cxp108] Copy to clipboard
Posted by: cxp108
Posted on: 2004-07-21 09:43

但是我在程序中用的都是setSelectedIndex(int)或者setSelectedItem(Object)

这两个方法,它们在ActionEvnet中的ActionCommand好像都是comboBoxChanged


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923