我的 jComboBox 反应不正常

发布于 2024-10-16 10:14:30 字数 1008 浏览 4 评论 0原文

我有一个可编辑的 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 技术交流群。

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

发布评论

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

评论(1

提笔落墨 2024-10-23 10:14:30

不要替换组合框模型,而是尝试仅更新模型。

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();
                DefaultComboBoxModel model = jComboBoxReceiver.getModel();
                model.removeAllElements();
                for(int i=0;i<usrList.size();i++){
                    model.addElement(usrList.get(i).getUserName());
                }
            }
        }         
    };

当您设置模型时,您正在重置 JComboBox 的世界视图。特别是,当模型被替换时,选择被设置为模型的所选项目。默认情况下这是第一项。就你而言,“弗雷德”。这将替换用户在组合框编辑器中键入的任何内容。

Instead of replacing the combo box model, try just updating the model.

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();
                DefaultComboBoxModel model = jComboBoxReceiver.getModel();
                model.removeAllElements();
                for(int i=0;i<usrList.size();i++){
                    model.addElement(usrList.get(i).getUserName());
                }
            }
        }         
    };

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文