Вы должны использовать ActionListener :
firstcombobox.addActionListener(new ActionListener(){
void actionPerformed(ActionEvent e){
// sets the selected item of secondcombobox to be the value of firstcombobox
// assuming secondcombobox contains such a value.
secondcombobox.setSelectedItem(firstcombobox.getSelecteditem());
}
});
Обратите внимание, что здесь важна область видимости. Вы можете сделать firstcombobox
и secondcombobox
глобальными или финальными, или вы можете использовать слегка альтернативную форму, где вы берете эти аргументы в качестве входных данных для конструктора:
firstcombobox.addActionListener(new ActionListener(firstcombobox, secondcombobox){
private JComboBox a;
private JComboBox b;
public ActionListner(JComboBox a, JComboBox b){
this.a = a;
this.b = b;
}
void actionPerformed(ActionEvent e){
// sets the selected item of a to be the value of b
// assuming a contains such a value.
b.setSelectedItem(a.getSelecteditem());
}
});