2010-12-19 138 views
0

我想在基于for和if循环的ArrayList中设置JCombobox中的值。JComboBox没有显示结果

 //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()); 
    } 

} 

但是组合框保持空白。这是为什么 ?

+0

实际上,它可能不是空的(但相当小,如果* Salesman.getCode()。toString()*返回空:)) – 2010-12-19 13:03:23

回答

0

您是否将组合框添加到其父容器?

只要有一些推销员在场,上面的代码应该可以正常工作。

你说你确定公司不是对象,但你有没有检查你实际添加到ComboBoxModel的内容?此外,您不需要实例化自己的DefaultComboBoxModel,因为您可以通过JComboBox“免费”获得一个DefaultcomboBoxModel

+0

我写了一个测试方法,打印“当前”的值。 当我调用另一个JFrame调用这个方法(通过使用AddOrder.test();它打印我两个代码(45,46) – George 2010-12-19 13:03:08

0

如果您只是将一些测试字符串添加到JComboBox,它们是否显示?

如果它们是.getCode()方法可能有问题。

此外,请尝试删除DefaultComboBoxModel,因为这不是必需的。

+0

如果手动添加salesman.addItem(“blablabla”);,它getCode()除了返回推销员密码外什么都没做, – George 2010-12-19 14:17:57

2

无论如何,我建议你以下:

  1. 覆盖的的toString()推销员类的方法以具有期望的可视化表示(即代码)。

  2. 添加推销员,而不是代码组合框模型

1

将项目添加到模型不直接到组合框。另外,请使用barti_ddu的建议。

0

好问题已解决!我只是向该类的构造函数添加了一个Company类对象,该对象使用setCompany进行设置。

+0

你设法解决你的问题很好,现在你需要将其中一个答案标记为你接受的答案 - 或者标记你自己的答案,你发现最有用的。 – BoffinbraiN 2010-12-23 15:43:30