George
George

Reputation: 433

JComboBox not showing results

i am trying to set the values in a JCombobox based on a for and if loop, on an arraylist.

     //salesman=the name of the combobox
    salesman = new javax.swing.JComboBox();

DefaultComboBoxModel model = new DefaultComboBoxModel();
 salesman.setModel(model);

/*company is an object of Company class, that gets set with a setter method to ensure 
that the gui will be pointing to the right object. and it does contains the data i want, so 
i am sure that company is not the problem */       
//loop tp set box list 
for (Employee current : company.getArray()){

 if (current instanceof Salesman) {
   salesman.addItem(current.getCode());
  }

}

but the combobox stays empty. why is that ?

Upvotes: 0

Views: 2001

Answers (5)

George
George

Reputation: 433

OK PROBLEM SOLVED! i just added to the constructor of the class a Company class object, that gets set with setCompany.

Upvotes: 0

jzd
jzd

Reputation: 23629

Add the items to the model not directly to the ComboBox. Also, use barti_ddu's recommendations as well.

Upvotes: 1

barti_ddu
barti_ddu

Reputation: 10299

Anyway, I'd suggest you the following:

  1. Override the toString() method of Salesman class to have desirable visual representation (i.e. code).

  2. Add Salesmen, not codes to combo box model.

Upvotes: 2

Tom W
Tom W

Reputation: 578

If you just add some test Strings to the JComboBox, are they displayed?

If they are it might be something wrong with the .getCode() method.

Also, try removing the DefaultComboBoxModel, because this isn't required.

Upvotes: 0

tddmonkey
tddmonkey

Reputation: 21184

Are you adding the combobox to its parent container?

The above code should work fine as long as there are some instances of Salesman present.

You say you're sure the company is not the object but have you checked what you're actually adding to the ComboBoxModel? Also, you don't need instantiate your own DefaultcomboBoxModel as you get one "for free" with the JComboBox

Upvotes: 0

Related Questions