Hi.
I have two tables in mysql - customer_master and contact_master.
customer_master has a primary key - "cust_id" ,which is a foreign key in contact_master and "cust_name" which is also a foreign key in contact_master.
I have a frame which will do CRUD operations for contact_master.
The frame has two two comboboxes, one for "cust_id" and other for "cust_name".
When the form loads, the comboboxes are filled with the resultset which contains records <cust_id, cust_name > retrieved from cutomer_master.
Now, when I select a particular cust_id from the first combobox, the second combobox should reflect the corresponding cust_name and vice-versa
I tried using both ActionPerformed and ItemChanged events.
Here's my code:
private void reset_cont_master_Fields(){
Cust_ID_Cont_Mstr_ComboBox.removeAllItems();//remove all previous items
Company_Cont_Mstr_ComboBox.removeAllItems();//from the lists
try {//since these are combo boxes, query has to execeuted and resultset filled into them
R2 = S.executeQuery("select cust_id, cust_name from Customer_master order by cust_id;");
//R.first();
while (R2.next()) {
Cust_ID_Cont_Mstr_ComboBox.addItem(R2.getString("cust_id"));
Company_Cont_Mstr_ComboBox.addItem(R2.getString("cust_name"));
}
} catch (SQLException e) {SQL_fetch_error_Dialog.where_error_has_occured = e.getMessage();//flash error..
SQL_fetch_error_Dialog.main(args);System.out.println(e);
}//catch ends
This is the ItemStateChanged event:
private void Cust_ID_Cont_Mstr_ComboBoxItemStateChanged(java.awt.event.ItemEvent evt) {
//System.out.println(Cust_ID_Cont_Mstr_ComboBox.getSelectedItem().toString());
int indx = Cust_ID_Cont_Mstr_ComboBox.getSelectedIndex();
Company_Cont_Mstr_ComboBox.setSelectedIndex(indx);
}
When reset_cont_master_Fields() is called, I get
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: setSelectedIndex: 0 out of bounds
at javax.swing.JComboBox.setSelectedIndex(JComboBox.java:601)
at masterentry.MasterEntry_Frame.Cust_ID_Cont_Mstr_ComboBoxItemStateChanged(MasterEntry_Frame.java:1031)
at masterentry.MasterEntry_Frame.access$900(MasterEntry_Frame.java:23)
at masterentry.MasterEntry_Frame$12.itemStateChanged(MasterEntry_Frame.java:565)
at javax.swing.JComboBox.fireItemStateChanged(JComboBox.java:1205)
at javax.swing.JComboBox.selectedItemChanged(JComboBox.java:1262)
at javax.swing.JComboBox.contentsChanged(JComboBox.java:1309)
at javax.swing.AbstractListModel.fireContentsChanged(AbstractListModel.java:100)
at javax.swing.DefaultComboBoxModel.setSelectedItem(DefaultComboBoxModel.java:88)
at javax.swing.DefaultComboBoxModel.addElement(DefaultComboBoxModel.java:126)
at javax.swing.JComboBox.addItem(JComboBox.java:696)
at masterentry.MasterEntry_Frame.reset_cont_master_Fields(MasterEntry_Frame.java:927)
What shud I do?
thanks..