我的 jComboBox 不对我的 keyListener 做出反应,并且 actionPerform 执行奇怪的操作
代码
public void actionPerformed(java.awt.event.ActionEvent e) {
sr = new Search(((String) jComboBoxReceiver.getSelectedItem()));
usrList = sr.searchUser();
String[] userList = new String[usrList.size()] ;
for(int i=0;i<usrList.size();i++){
userList[i]= usrList.get(i).getUserName();
}
model = new DefaultComboBoxModel(userList);
jComboBoxReceiver.setModel(model);
}
我正在尝试搜索 UserName 并将值返回到 jComboBox 上,这是单击其他位置或单击 Enter 后的 ,它将进行搜索,但是,它将再次搜索第一项,这非常令人困惑。 ..然后我尝试使用按下的键
if(e.getKeyCode()==13){
sr = new Search(((String) jComboBoxReceiver.getSelectedItem()));
usrList = sr.searchUser();
String[] userList = new String[usrList.size()] ;
for(int i=0;i<usrList.size();i++){
userList[i]= usrList.get(i).getUserName();
}
model = new DefaultComboBoxModel(userList);
jComboBoxReceiver.setModel(model);
}
,而这个根本没有反应。
I am trying to search for UserName and return values onto jComboBox, here is the code
public void actionPerformed(java.awt.event.ActionEvent e) {
sr = new Search(((String) jComboBoxReceiver.getSelectedItem()));
usrList = sr.searchUser();
String[] userList = new String[usrList.size()] ;
for(int i=0;i<usrList.size();i++){
userList[i]= usrList.get(i).getUserName();
}
model = new DefaultComboBoxModel(userList);
jComboBoxReceiver.setModel(model);
}
after you click to somewhere else or click enter,it will conduct the search, however, it will go search for the first item again, which is very confusing... then i tried using key Pressed
if(e.getKeyCode()==13){
sr = new Search(((String) jComboBoxReceiver.getSelectedItem()));
usrList = sr.searchUser();
String[] userList = new String[usrList.size()] ;
for(int i=0;i<usrList.size();i++){
userList[i]= usrList.get(i).getUserName();
}
model = new DefaultComboBoxModel(userList);
jComboBoxReceiver.setModel(model);
}
And this one does not react at all.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要在编辑器而不是组合框本身上设置侦听器。请参阅此处的答案:
检测用户何时在 Java 中按下 Enter
You need to set the listener(s) on the Editor not the ComboBox itself. See the answer here:
Detecting when user presses enter in Java
哇,你每次都重建一个 ComboBoxModel 吗?是不是有点贵啊?您知道有一个
MutableComboBoxModel
,也由DefaultComboBoxModel
实现,允许您从组合框中添加/删除元素,而无需每次都重建其模型?关于你的问题,我不明白这个说法
您的意思是您的 JComboBox 开始闪烁,内容每次都被修改?
如果是这样,可能是因为您的
ActionListener
链接到JComboBox
,其内容不断变化。无论如何,我建议您添加一些日志,例如
此外,我倾向于建议您另一种方法,其中组合框模型不包含简单的字符串,而是包含用户对象,带有
ListCellRenderer
仅显示用户名。Wow, you're rebuilding a ComboBoxModel each time ? Isn't it a little expensive ? You know there is a
MutableComboBoxModel
, also implemented byDefaultComboBoxModel
that would allow you to add/remove elements from you combobox without rebuilding its model each time ?Concerning your question, I don't understand the statement
Do you mean your JComboBox starts to blink with content being modified each time ?
if so, maybe is it because your
ActionListener
is linked toJComboBox
, which content changes continuously.Anyway, i suggest you add some logs, like
Besides, i would tend to suggest you an other approach, in which combo box model don't contain simple Strings, but rather User objects, with a
ListCellRenderer
displaying only the user name.IMO,真正让用户感到困惑的是,一旦他们选择其中一个选项,组合框的内容和选择就会发生变化。
无论如何,如果您确实想这样做,那么您应该在更改操作侦听器的内容之前删除它(或停用它),并在之后重新添加它(或重新激活它):
IMO, what will really be confusing for your users is to have the content and selection of a combo box changed as soon as they select one of its options.
Anyway, if you really want to do that, then you should remove the action listener (or deactivate it) before changing its content, and re-add it (or reactivate it) after :