java如何在同一个itemListener函数中处理2个不同的组合框itemListener?
我有两个组合框
JcomboBox1.addItemListener(this)
jComboBox2.addItemListener(this)
如何在同一个itemListener函数中处理这些问题
我正在处理1个组合框,但需要处理两个组合框
public void itemStateChanged(ItemEvent ie) {
String Product=(String)jComboBox1.getSelectedItem();
ResultSet rs=db.GetPriceOfaProduct(Product);
try{
rs.next();
int price=rs.getInt("pPrice");
jLabel6.setText("Amount Per Item is "+price);
}catch(Exception e)
{
System.out.println("Error in Taking Product Price");
}
# 1 楼答案
使用
ItemEvent.getSource()
检查哪些JComboBox
更改了选择请注意更改选择时,项目侦听器将收到两次通知,一次是关于取消选择的项目,另一次是关于所选项目强>
# 2 楼答案
您可以使用
JComboBox comboBox = (JComboBox) ie.getItem();
,而不是使用comboBox1.getSelectedItem()
或comboBox2.getSelectedItem()
,这样您就可以引用触发事件的JComboBox使用
getSource()
而不是getItem()