如何在同一个 itemlistener 函数中处理 2 个不同的组合框 itemListener?
how to handle 2 different combobox itemListener in same itemlistener function?
我有两个组合框
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");
}
您可以使用 JComboBox comboBox = (JComboBox) ie.getItem();
而不是使用 comboBox1.getSelectedItem()
或 comboBox2.getSelectedItem()
- 然后您可以引用触发事件的任何 JComboBox。
使用 getSource()
而不是 getItem()
使用 ItemEvent.getSource()
检查哪个 JComboBox
更改了选择。
请注意更改选择,项目侦听器将收到两次通知,一次是在取消选择的项目上,另一次是在选择的项目上。
public static void main(String[] args) {
JFrame frame = new JFrame("test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(new Dimension(500, 500));
frame.getContentPane().setLayout(new GridLayout(5, 5));
JComboBox<String> comboBox1 = new JComboBox<>(new String[] { "11", "12" });
JComboBox<String> comboBox2 = new JComboBox<>(new String[] { "21", "22" });
ItemListener listener = (e) -> {
if (e.getStateChange() == ItemEvent.SELECTED) {
if (e.getSource() == comboBox1) {
System.out.printf("JComboBox 1 state changed: %s selected\n", e.getItem());
}
else if (e.getSource() == comboBox2) {
System.out.printf("JComboBox 2 state changed: %s selected\n", e.getItem());
}
}
};
comboBox1.addItemListener(listener);
comboBox2.addItemListener(listener);
frame.getContentPane().add(comboBox1);
frame.getContentPane().add(comboBox2);
frame.setVisible(true);
}
我有两个组合框
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");
}
您可以使用 JComboBox comboBox = (JComboBox) ie.getItem();
而不是使用 comboBox1.getSelectedItem()
或 comboBox2.getSelectedItem()
- 然后您可以引用触发事件的任何 JComboBox。
使用 getSource()
而不是 getItem()
使用 ItemEvent.getSource()
检查哪个 JComboBox
更改了选择。
请注意更改选择,项目侦听器将收到两次通知,一次是在取消选择的项目上,另一次是在选择的项目上。
public static void main(String[] args) {
JFrame frame = new JFrame("test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(new Dimension(500, 500));
frame.getContentPane().setLayout(new GridLayout(5, 5));
JComboBox<String> comboBox1 = new JComboBox<>(new String[] { "11", "12" });
JComboBox<String> comboBox2 = new JComboBox<>(new String[] { "21", "22" });
ItemListener listener = (e) -> {
if (e.getStateChange() == ItemEvent.SELECTED) {
if (e.getSource() == comboBox1) {
System.out.printf("JComboBox 1 state changed: %s selected\n", e.getItem());
}
else if (e.getSource() == comboBox2) {
System.out.printf("JComboBox 2 state changed: %s selected\n", e.getItem());
}
}
};
comboBox1.addItemListener(listener);
comboBox2.addItemListener(listener);
frame.getContentPane().add(comboBox1);
frame.getContentPane().add(comboBox2);
frame.setVisible(true);
}