2

I use JcomboBox as a suggestion box that when user type in, it check for matches and display suggestion. Here is how I create the JComboBox:

    Vector<String> popUpVector = new Vector<String>();
    JComboBox jcb = new JComboBox(popUpVector);

every time Key Listener catch event, I do this

    popUpVector.clear();
    jcb.hidhPopUp();
    for(String s : database){
     popUpVector.add(s);
    }
    jcb.showPopUp();

It works as long as I don't select item from the dropdown. However, once I select item from the dropdown, the dropDown will display blank afterward, I check the popUpVector, it is not empty though, I think it has something to do with the selection, so I unhook it from actionListener, it didn't helps.

Can anyone help me with this, thanks a lot!

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Ga Zhua
  • 107
  • 3
  • 14

2 Answers2

3

Passing a Vector to the JComboBox constructor will according to the source indeed use that vector to back the underlying model:

public JComboBox(Vector<?> items) {
    super();
    setModel(new DefaultComboBoxModel(items));
    init();
}

and

public DefaultComboBoxModel(Vector<?> v) {
    objects = v;

    if ( getSize() > 0 ) {
        selectedObject = getElementAt( 0 );
    }
}

Meaning that if you change the contents of the vector, you also change the contents of your model. However, making changes to the model requires to fire the correct events to inform the view about the changes. And since vector does not fire any events, the DefaultComboBoxModel has no way of knowing that the contents of the vector has been changed. So imo the DefaultComboBoxModel constructor simply should have taken the elements from the vector and store those iso storing the vector directly.

Now to solve your problem: instead of storing your values in a Vector, use a DefaultComboBoxModel and use the available API on that model to make the changes. Using the API will make sure the model fires the correct changes. See for example the implementation of the addElement method:

public void addElement(Object anObject) {
    objects.addElement(anObject);
    fireIntervalAdded(this,objects.size()-1, objects.size()-1);
    if ( objects.size() == 1 && selectedObject == null && anObject != null ) {
        setSelectedItem( anObject );
    }
}
Robin
  • 36,233
  • 5
  • 47
  • 99
  • Thanks for the explanations, I try the use the defaultModel now, and got another problem, it auto-complete whatever I type. Is that a way to disable this?? – Ga Zhua Sep 26 '12 at 07:56
  • @GaZhua As you can see from the constructor with the vector, using the vector or setting your own `DefaultComboBoxModel` is exactly the same, except that you have easy access to the model. So the auto-complete part was either already present with the vector, or you just added. But that is a different question, and not something I will try to answer in comments – Robin Sep 26 '12 at 08:12
1

your issue is

popUpVector.clear();

correct way to clear the Vector is only

popUpVector = new Vector<String>();

better could be to add / remove / modify the JComboBoxes Items in ComboBoxModel

Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • 1
    Creating a new vector is not the solution. In that case the pop-up will contain all the old values – Robin Sep 26 '12 at 07:34