[英]Selected combobox value added twice to an arraylist
I've populated the elements of a combo box using a SQL query and am writing the selected value to an arraylist. 我使用SQL查询填充了组合框的元素,并将所选值写入arraylist。 When I print out the elements of the arraylist, the value has been added twice.
当我打印出arraylist的元素时,该值已被添加两次。 Does anybody know why this is, and how do I stop this from happening?
有人知道这是为什么吗?如何阻止这种情况发生?
Extracts from the code: 从代码中摘录:
resultSet = statement.executeQuery("SELECT name FROM menu WHERE category = 'beverage'");
while (resultSet.next())
{
beverageJComboBox.addItem(resultSet.getString(1));
System.out.printf("%s", resultSet.getString(1));
}
And for the adding ite to the ArrayList: 对于将ite添加到ArrayList:
beverageJComboBox.addItemListener(
new ItemListener()
{
public void itemStateChanged( ItemEvent event )
{
billItems.add((String)beverageJComboBox.getSelectedItem());
System.out.printf("%s", billItems);
}
}// end anonymous inner class
);
(Very new to Java!) (对Java来说是新手!)
Use ActionListener instead of ItemListener
使用ActionListener而不是
ItemListener
beverageJComboBox.addActionListener (new ActionListener () {
public void actionPerformed(ActionEvent e) {
...
}
});
Note: use distinct
keyword in query itself to show the unique record in JComboBox
. 注意:在查询本身中使用
distinct
关键字可以显示JComboBox
的唯一记录。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.