我的 jComboBox 反应不正常
我有一个可编辑的 jCombobox ,如果您在其中输入内容并单击 Enter,它将搜索选定的用户,这是我的代码
jComboBoxReceiver.getEditor().getEditorComponent().addKeyListener(new java.awt.event.KeyAdapter() {
public void keyPressed(java.awt.event.KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_ENTER) {
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();
}
DefaultComboBoxModel modelList = new DefaultComboBoxModel(userList);
jComboBoxReceiver.setModel(modelList);
}
}
});
然后,例如,如果您输入 f,它应该返回 Fred 和 Fried Chicken,但是在找到之后结果,它会再次搜索 Fred,这本身就是第一项......谁能告诉我为什么?
I am having an editable jCombobox , and it will search for selected users if you enter something inside and click enter, this is my code
jComboBoxReceiver.getEditor().getEditorComponent().addKeyListener(new java.awt.event.KeyAdapter() {
public void keyPressed(java.awt.event.KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_ENTER) {
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();
}
DefaultComboBoxModel modelList = new DefaultComboBoxModel(userList);
jComboBoxReceiver.setModel(modelList);
}
}
});
And then, for example , if you type f, it should return Fred and Fried Chicken, but after it found the result, it will go search again for Fred which is the first item by itself... can anyone tell me why ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不要替换组合框模型,而是尝试仅更新模型。
当您设置模型时,您正在重置 JComboBox 的世界视图。特别是,当模型被替换时,选择被设置为模型的所选项目。默认情况下这是第一项。就你而言,“弗雷德”。这将替换用户在组合框编辑器中键入的任何内容。
Instead of replacing the combo box model, try just updating the model.
When you set the model, you are resetting the JComboBox's view of its world. In particular, when the model is replaced the selection is set to the model's selected item. This is by default the first item. In your case "Fred". This replaces whatever the user has typed in the combobox editor.