I have two classes, a gui class that contains two combobox’s and a listener class that listens to the two combobox’s. The software is about pizza’s and the combobox’s are to select pizza type and quantity, I’ll post the relevant code.
(gui class)
private Listener listen = new Listener();
private JComboBox chooseItem = new JComboBox();
private JComboBox quantity = new JComboBox();
private String[] selection = {"Choose a Pizza","Margherita", "Pepperoni", "Four Seasons", "Chips", "Garlic Bread", "Potato Wedges", "Cocacola", "Orange Juice", "Lemonade"};
private String[] qSelection = {"0","1","2","3","4","5","6","7","8","9","10"};
private void comboBoxs() {
choosePizza = new JComboBox(selection);
chooseItem.setSelectedIndex(0);
panel.add(choosePizza);
choosPizza.addActionListener(listener);
quantity = new JComboBox(qSelection);
quantity.setSelectedIndex(0);
panel.add(quantity);
quantity.addActionListener(listener);
}
In the listener class I am stuck and have not been able to find a solution as of yet. I have to use an ActionListener but I am unsure as to how I am meant to differentiate between the two combobox’s.
(listener class)
public void actionPerformed(ActionEvent event) {
if (actionCommand.equals("Confirm")) {
String q = new String();
String d = new String();
d = description;
q = quantity;
}
else {
JComboBox cbq = (JComboBox)event.getSource();
String itemNumber = (String)cbq.getSelectedItem();
getQuantity(itemNumber);
JComboBox cb = (JComboBox)event.getSource();
String name = (String)cb.getSelectedItem();
getItemName(name);
}
}
public String getItemName(String name) {
description = new String();
description = name;
return name;
}
public String getQuantity(String itemNumber){
quantity = new String();
quantity = itemNumber;
return itemNumber;
}
So, to summarize: when I select values in the two JComboBox’s I want to be able to store the selected chooseItem JComboBox item in the “description” string and the selected quantity JComboBox item in the “quantity” string.
Currently the chooseItem JComboBox selected item appears as both values and the quantity does not appear at all. (This is actually progress as before whichever box was used last appeared in the description string and quantity pointed to null.)
All the examples I’ve been able to find online only ever involve an ActionListener with a single JComboBox, I’ve been trying to get it to work for hours but have had no luck, I’m completely stuck.
You either attach a separate listener to each combobox, so your listener knows that the events its receives originated from that specific combobox.
The other solution is to give the listener access to those comboboxes (either by exposing them in your class, or by passing them as parameters to the constructor of your listener), and compare the source of the event with those comboboxes to determine which combobox caused the event.
Personally I prefer a one-to-one mapping between my listeners and my components to avoid such complications.
Small side-note: there are some weird things in the code you posted
public void getQuantity(String itemNumber): thevoidconflicts with the fact you actually return something -> won’t compileIn your listener you ask the event twice for its source and cast it to two different comboboxes. This is of course incorrect