次のように、forループでコンボボックスの配列を作成します。
for(int i = 0; i < 5; i++) {
...
comboStudy[i] = new JComboBox(studyModel);
comboStudy[i].addActionListener(new studyListener());
comboStudy[i].setActionCommand("" + i);
...
}
リスナーはインスタンスの内部クラスです。
public class studyListener implements ActionListener {
public void actionPerformed(ActionEvent evt) {
int i = Integer.parseInt(evt.getActionCommand());
// do some stuff that requires i and also access
// to the instance members of the containing class
}
}
私が今直面している問題は、comboStudy [0]で実行時に選択を行うたびに、アクションイベントが5回発生することです。初めてiが4になると、0になるまで毎回減少します。
ItemListenerを使用して試しましたが、同じ問題があります。
助けてください!