开发者

JComboBox not showing results

开发者 https://www.devze.com 2023-01-31 12:51 出处:网络
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

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 ?


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.


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


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


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.


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

0

精彩评论

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

关注公众号