开发者

my jComboBox does not react to my keyListener and actionPerform performs weired stuff

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 o开发者_开发百科r 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.


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


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.


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);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜