I have tried to use the suggested methods but unable to determine how I can use action listeners within an action listener as suggested...
I want to change the value of first combo box and want the next combo box to be updated automatically upon change, similarly text box is changed when combobox_1 is changed...
String[] b = a.getCourseCodes();
final List f = new ArrayList();
final JComboBox comboBox = new JComboBox(b);
comboBox.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String item = comboBox.getSelectedItem().toString();
}
});
comboBox.setEditable(true);
comboBox.setBounds(360, 70, 86, 20);
contentPane.add(comboBox);
JLabel lblStudentName = new JLabel("Student Name");
lblStudentName.setBounds(270, 149, 80, 14);
contentPane.add(lblStudentName);
String[] v = a.getStudentID(comboBox.getSelectedItem().toString());
final JComboBox comboBox_1 = new JComboBox(v);
comboBox_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String item = comboBox_1.getSelectedItem().toString();
}
});
comboBox_1.setBounds(360, 108, 86, 20);
contentPane.add(comboBox_1);
textField_3 = new JTextField();
String y = a.getStudentName(comboBox_1.getSelectedItem().toString());
textField_3.setText(y);
textField_3.setEditable(false);
textField_3.setBounds(360, 146, 86, 20);
contentPane.add(textField_3);
textField_3.setColumns(10);
Kindly help by editing the code so can have a clear idea... Thanks
Rather then adding an ItemListener
I would simply add an ActionListener
which will be triggered every time the selected value is changed. Then you are able to just use comboBox.getSelectedItem()
like so:
JComboBox comboBox_1; //you need to declare the comboBox and textField before the ActionListener.
comboBox.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String[] v = a.getStudentID(comboBox.getSelectedItem().toString());
comboBox_1.setModel(new DefaultComboBoxModel<String>(v));
String y = a.getStudentName(comboBox_1.getSelectedItem().toString());
textField_3.setText(y);
}
});
Add you could extend this to change the values of your ComboBox
or TextField
within the actionPerformed
method.
I think this is what you mean, though I may be wrong in the intent of your ActionListener
.