My app has got an array of JComboBox named comboBox and they have actionListeners attached. The question is how do I determine the array index of the comboBox that fired the event?
I need to know so that I can transfer the chosen value in comboBox to a JTextField with an identical array index.
You can get the reference to the currently selected JComboBox via the actionPerformed method's ActionEvent parameter by calling getSource() on it. Then you can iterate through the combo box array using this object to determine which array item is the selected combo box. For example:
public void actionPerformed(ActionEvent e)
JComboBox selectedComboBox = (JComboBox)e.getSource();
int comboIndex = -1;
for (int i = 0; i < comboBox.length; i++)
if (comboBox[i] == selectedComboBox)
comboIndex = i;
// do what needs to be done with comboIndex here
Oh, no! Fubarable's been promoted!
You can also use the "name" property of each of the components. It's not normally set, but you can use that to keep them straight without having to look through an array.