我正在尝试搜索 UserName 并将值返回到 jComboBox,这里是代码
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);
}
当你点击其他地方或者点击回车后,它会进行搜索,但是,它会再次搜索第一个项目,这很困惑......然后我尝试使用 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);
}
而这个根本没有反应。
最佳答案
您需要在编辑器而不是组合框本身上设置监听器。请参阅此处的答案:
关于java - 我的 jComboBox 不对我的 keyListener 使用react,并且 actionPerform 执行奇怪的操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4864335/