我的 jComboBox 不对我的 keyListener 做出反应,并且 actionPerform 执行奇怪的操作

发布于 2024-10-15 10:21:50 字数 964 浏览 6 评论 0原文

代码

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

◇流星雨 2024-10-22 10:21:50

您需要在编辑器而不是组合框本身上设置侦听器。请参阅此处的答案:

检测用户何时在 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

霊感 2024-10-22 10:21:50

哇,你每次都重建一个 ComboBoxModel 吗?是不是有点贵啊?您知道有一个 MutableComboBoxModel ,也由 DefaultComboBoxModel 实现,允许您从组合框中添加/删除元素,而无需每次都重建其模型?

关于你的问题,我不明白这个说法

但是,如果我这样做,它确实会正确执行,但是,它会再次搜索第一个项目

您的意思是您的 JComboBox 开始闪烁,内容每次都被修改?

如果是这样,可能是因为您的 ActionListener 链接到 JComboBox,其内容不断变化。

无论如何,我建议您添加一些日志,例如

sr = new Search(((String) jComboBoxReceiver.getSelectedItem()));    
DefaultComboBoxModel model = (DefaultComboBoxModel) jComboBoxReceiver.getModel();
model.remvoeAllElements();
usrList = sr.searchUser();
String[] userList = new String[usrList.size()] ;
for(int i=0;i<usrList.size();i++){
    String username = usrList.get(i).getUserName();
    System.out.println(username); // feel free to instead use one loger
    model.addElement(username);
}

此外,我倾向于建议您另一种方法,其中组合框模型不包含简单的字符串,而是包含用户对象,带有 ListCellRenderer 仅显示用户名。

Wow, you're rebuilding a ComboBoxModel each time ? Isn't it a little expensive ? You know there is a MutableComboBoxModel, also implemented by DefaultComboBoxModel 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

However, if i do that, it does perform correctly, however, it will go search for the first item again

Do you mean your JComboBox starts to blink with content being modified each time ?

if so, maybe is it because your ActionListener is linked to JComboBox, which content changes continuously.

Anyway, i suggest you add some logs, like

sr = new Search(((String) jComboBoxReceiver.getSelectedItem()));    
DefaultComboBoxModel model = (DefaultComboBoxModel) jComboBoxReceiver.getModel();
model.remvoeAllElements();
usrList = sr.searchUser();
String[] userList = new String[usrList.size()] ;
for(int i=0;i<usrList.size();i++){
    String username = usrList.get(i).getUserName();
    System.out.println(username); // feel free to instead use one loger
    model.addElement(username);
}

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.

不醒的梦 2024-10-22 10:21:50

IMO,真正让用户感到困惑的是,一旦他们选择其中一个选项,组合框的内容和选择就会发生变化。

无论如何,如果您确实想这样做,那么您应该在更改操作侦听器的内容之前删除它(或停用它),并在之后重新添加它(或重新激活它):

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.removeActionListener(this);
    jComboBoxReceiver.setModel(model);
    jComboBoxReceiver.addActionListener(this);
}

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 :

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.removeActionListener(this);
    jComboBoxReceiver.setModel(model);
    jComboBoxReceiver.addActionListener(this);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文