JComboBox help

Reply

Join Date: Dec 2007
Posts: 117
Reputation: abhi_elementx is an unknown quantity at this point 
Solved Threads: 7
abhi_elementx's Avatar
abhi_elementx abhi_elementx is offline Offline
Junior Poster

JComboBox help

 
0
  #1
Apr 10th, 2009
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:
  1. private void reset_cont_master_Fields(){
  2. Cust_ID_Cont_Mstr_ComboBox.removeAllItems();//remove all previous items
  3. Company_Cont_Mstr_ComboBox.removeAllItems();//from the lists
  4.  
  5. try {//since these are combo boxes, query has to execeuted and resultset filled into them
  6.  
  7. R2 = S.executeQuery("select cust_id, cust_name from Customer_master order by cust_id;");
  8. //R.first();
  9. while (R2.next()) {
  10. Cust_ID_Cont_Mstr_ComboBox.addItem(R2.getString("cust_id"));
  11. Company_Cont_Mstr_ComboBox.addItem(R2.getString("cust_name"));
  12. }
  13. } catch (SQLException e) {SQL_fetch_error_Dialog.where_error_has_occured = e.getMessage();//flash error..
  14. SQL_fetch_error_Dialog.main(args);System.out.println(e);
  15. }//catch ends

This is the ItemStateChanged event:
  1. private void Cust_ID_Cont_Mstr_ComboBoxItemStateChanged(java.awt.event.ItemEvent evt) {
  2. //System.out.println(Cust_ID_Cont_Mstr_ComboBox.getSelectedItem().toString());
  3. int indx = Cust_ID_Cont_Mstr_ComboBox.getSelectedIndex();
  4. Company_Cont_Mstr_ComboBox.setSelectedIndex(indx);
  5. }

When reset_cont_master_Fields() is called, I get
  1. Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: setSelectedIndex: 0 out of bounds
  2. at javax.swing.JComboBox.setSelectedIndex(JComboBox.java:601)
  3. at masterentry.MasterEntry_Frame.Cust_ID_Cont_Mstr_ComboBoxItemStateChanged(MasterEntry_Frame.java:1031)
  4. at masterentry.MasterEntry_Frame.access$900(MasterEntry_Frame.java:23)
  5. at masterentry.MasterEntry_Frame$12.itemStateChanged(MasterEntry_Frame.java:565)
  6. at javax.swing.JComboBox.fireItemStateChanged(JComboBox.java:1205)
  7. at javax.swing.JComboBox.selectedItemChanged(JComboBox.java:1262)
  8. at javax.swing.JComboBox.contentsChanged(JComboBox.java:1309)
  9. at javax.swing.AbstractListModel.fireContentsChanged(AbstractListModel.java:100)
  10. at javax.swing.DefaultComboBoxModel.setSelectedItem(DefaultComboBoxModel.java:88)
  11. at javax.swing.DefaultComboBoxModel.addElement(DefaultComboBoxModel.java:126)
  12. at javax.swing.JComboBox.addItem(JComboBox.java:696)
  13. at masterentry.MasterEntry_Frame.reset_cont_master_Fields(MasterEntry_Frame.java:927)

What shud I do?
thanks..
PEACE !
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 1,180
Reputation: JamesCherrill is a glorious beacon of light JamesCherrill is a glorious beacon of light JamesCherrill is a glorious beacon of light JamesCherrill is a glorious beacon of light JamesCherrill is a glorious beacon of light 
Solved Threads: 171
JamesCherrill JamesCherrill is offline Offline
Veteran Poster

Re: JComboBox help

 
0
  #2
Apr 10th, 2009
The exception is thrown when the index is >= size, so it looks like there is nothing in Company_Cont_Mstr_ComboBox when this code is executed. (Maybe? happens when first cb is populated = fires change event, before 2nd is populated? Test size of Company_Cont_Mstr_ComboBox before setting its index in the change listener?)
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 117
Reputation: abhi_elementx is an unknown quantity at this point 
Solved Threads: 7
abhi_elementx's Avatar
abhi_elementx abhi_elementx is offline Offline
Junior Poster

Re: JComboBox help

 
0
  #3
Apr 10th, 2009
OK...I solved the problem, but partially..
When the frame is loaded for the first time, it populates the comboboxes properly...
But when I click on 'New' button, which shoud remove all the previous items and reexecute the query to refetchthe records, I get some exceptions at the point where it tries to remove items from "Cust_id" combobox..

Here's my code:
  1. private void reset_cont_master_Fields(){Cust_ID_Cont_Mstr_ComboBox.removeAllItems();//
  2. Cust_ID_Cont_Mstr_ComboBox.removeAllItems();//remove all previous items
  3. Company_Cont_Mstr_ComboBox.removeAllItems();//from the lists
  4.  
  5. try {//since these are combo boxes, query has to execeuted and resultset filled into them
  6. flag_betn_Comboxs = 0;
  7. R2 = S.executeQuery("select cust_id, cust_name from Customer_master order by cust_id;");
  8. //R.first();
  9. while (R2.next()) {
  10. Cust_ID_Cont_Mstr_ComboBox.addItem(R2.getString("cust_id"));
  11. Company_Cont_Mstr_ComboBox.addItem(R2.getString("cust_name"));
  12. }
  13. flag_betn_Comboxs = 1;//This indicates all the items have been filled and thus dues not cause ItemState-
  14. //Changed event to be triggered everytime an item is added.
  15.  
  16. } catch (SQLException e) {SQL_fetch_error_Dialog.where_error_has_occured = e.getMessage();//flash error..
  17. SQL_fetch_error_Dialog.main(args);System.out.println(e);
  18. }//catch ends

Here's the NewButtonClick event:
  1. private void New_Cont_Mstr_ButtonMouseClicked(java.awt.event.MouseEvent evt) {
  2. SaveFlag2 = 0;//insert query can be executed..
  3. reset_cont_master_Fields();//Blank the fields
  4. }

Here are the actionlisteners
  1. private void Cust_ID_Cont_Mstr_ComboBoxActionPerformed(java.awt.event.ActionEvent evt) {
  2. if(flag_betn_Comboxs == 0)return;//if all items are not filled, dont let control go down..
  3.  
  4. //int indx = Cust_ID_Cont_Mstr_ComboBox.getSelectedIndex();
  5. // Company_Cont_Mstr_ComboBox.setSelectedIndex(indx);
  6. try {
  7. R2.beforeFirst();//beforefirst();
  8.  
  9. while (R2.next()) {
  10. if (Cust_ID_Cont_Mstr_ComboBox.getSelectedItem().equals(R2.getString(1))) {
  11. Company_Cont_Mstr_ComboBox.setSelectedItem(R2.getString(2));
  12. break;
  13. }//if ends
  14. }//while ends
  15. } catch (SQLException sQLException) {}
  16.  
  17. //flag_betn_Comboxs = 0;//reset flag
  18. }
  19.  
  20. private void Company_Cont_Mstr_ComboBoxActionPerformed(java.awt.event.ActionEvent evt) {
  21. if(flag_betn_Comboxs == 0)return;//if all items are not filled, dont let control go down..
  22. try {
  23. R2.beforeFirst();//beforefirst();
  24.  
  25. while (R2.next()) {
  26. if (Company_Cont_Mstr_ComboBox.getSelectedItem().equals(R2.getString(2))) {
  27. Cust_ID_Cont_Mstr_ComboBox.setSelectedItem(R2.getString(1));
  28. break;
  29. }
  30. }//while ends
  31. } catch (SQLException sQLException) {}
  32.  
  33. //flag_betn_Comboxs = 0;//reset flag
  34. }

Here's the exception:
  1. Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
  2. at masterentry.MasterEntry_Frame.Cust_ID_Cont_Mstr_ComboBoxActionPerformed(MasterEntry_Frame.java:1002)
  3. at masterentry.MasterEntry_Frame.access$800(MasterEntry_Frame.java:24)
  4. at masterentry.MasterEntry_Frame$11.actionPerformed(MasterEntry_Frame.java:567)
  5. at javax.swing.JComboBox.fireActionEvent(JComboBox.java:1240)
  6. at javax.swing.JComboBox.contentsChanged(JComboBox.java:1311)
  7. at javax.swing.JComboBox.intervalRemoved(JComboBox.java:1331)
  8. at javax.swing.AbstractListModel.fireIntervalRemoved(AbstractListModel.java:161)
  9. at javax.swing.DefaultComboBoxModel.removeAllElements(DefaultComboBoxModel.java:169)
  10. at javax.swing.JComboBox.removeAllItems(JComboBox.java:751)
  11. at masterentry.MasterEntry_Frame.reset_cont_master_Fields(MasterEntry_Frame.java:917)
  12. at masterentry.MasterEntry_Frame.New_Cont_Mstr_ButtonMouseClicked(MasterEntry_Frame.java:954)
  13. at masterentry.MasterEntry_Frame.access$400(MasterEntry_Frame.java:24)
  14. at masterentry.MasterEntry_Frame$7.mouseClicked(MasterEntry_Frame.java:536)
  15. at java.awt.AWTEventMulticaster.mouseClicked(AWTEventMulticaster.java:253)
  16. at java.awt.Component.processMouseEvent(Component.java:6219)
  17. at javax.swing.JComponent.processMouseEvent(JComponent.java:3265)
  18. at java.awt.Component.processEvent(Component.java:5981)
  19. at java.awt.Container.processEvent(Container.java:2041)
  20. at java.awt.Component.dispatchEventImpl(Component.java:4583)
  21. at java.awt.Container.dispatchEventImpl(Container.java:2099)
  22. at java.awt.Component.dispatchEvent(Component.java:4413)
  23. at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4556)
  24. at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4229)
  25. at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4150)
  26. at java.awt.Container.dispatchEventImpl(Container.java:2085)
  27. at java.awt.Window.dispatchEventImpl(Window.java:2475)
  28. at java.awt.Component.dispatchEvent(Component.java:4413)
  29. at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
  30. at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
  31. at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
  32. at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
  33. at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
  34. at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
  35. at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

When i click on New for the second time, it generates this:
  1. Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
  2. at masterentry.MasterEntry_Frame.Company_Cont_Mstr_ComboBoxActionPerformed(MasterEntry_Frame.java:1018)
  3. at masterentry.MasterEntry_Frame.access$900(MasterEntry_Frame.java:24)
  4. at masterentry.MasterEntry_Frame$12.actionPerformed(MasterEntry_Frame.java:573)
  5. at javax.swing.JComboBox.fireActionEvent(JComboBox.java:1240)
  6. at javax.swing.JComboBox.contentsChanged(JComboBox.java:1311)
  7. at javax.swing.JComboBox.intervalRemoved(JComboBox.java:1331)
  8. at javax.swing.AbstractListModel.fireIntervalRemoved(AbstractListModel.java:161)
  9. at javax.swing.DefaultComboBoxModel.removeAllElements(DefaultComboBoxModel.java:169)
  10. at javax.swing.JComboBox.removeAllItems(JComboBox.java:751)
  11. at masterentry.MasterEntry_Frame.reset_cont_master_Fields(MasterEntry_Frame.java:919)
  12. at masterentry.MasterEntry_Frame.New_Cont_Mstr_ButtonMouseClicked(MasterEntry_Frame.java:954)
  13. at masterentry.MasterEntry_Frame.access$400(MasterEntry_Frame.java:24)
  14. at masterentry.MasterEntry_Frame$7.mouseClicked(MasterEntry_Frame.java:536)
  15. at java.awt.AWTEventMulticaster.mouseClicked(AWTEventMulticaster.java:253)
  16. at java.awt.Component.processMouseEvent(Component.java:6219)
  17. at javax.swing.JComponent.processMouseEvent(JComponent.java:3265)
  18. at java.awt.Component.processEvent(Component.java:5981)
  19. at java.awt.Container.processEvent(Container.java:2041)
  20. at java.awt.Component.dispatchEventImpl(Component.java:4583)
  21. at java.awt.Container.dispatchEventImpl(Container.java:2099)
  22. at java.awt.Component.dispatchEvent(Component.java:4413)
  23. at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4556)
  24. at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4229)
  25. at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4150)
  26. at java.awt.Container.dispatchEventImpl(Container.java:2085)
  27. at java.awt.Window.dispatchEventImpl(Window.java:2475)
  28. at java.awt.Component.dispatchEvent(Component.java:4413)
  29. at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
  30. at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
  31. at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
  32. at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
  33. at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
  34. at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
  35. at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

When I click New for the third time, IT WORKS AS IT IS SUPPOSED TO!
Thanks...
PEACE !
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 1,180
Reputation: JamesCherrill is a glorious beacon of light JamesCherrill is a glorious beacon of light JamesCherrill is a glorious beacon of light JamesCherrill is a glorious beacon of light JamesCherrill is a glorious beacon of light 
Solved Threads: 171
JamesCherrill JamesCherrill is offline Offline
Veteran Poster

Re: JComboBox help

 
0
  #4
Apr 10th, 2009
It's hard to give help on these exceptions because (a) the listing is truncated and I can't see the line number for the critical error, and (b) without the program line numbers on the program listing I can't match up the exception to the statement that caused it. Null pointer exception means something is un-initialised or possible something is empty.
However, please try to interpret the exceptions yourself. The first line of the stack dump shows you the statement where it failed, and the text of the exception (first line) tells you what went wrong. 90% of the time that's all you need, and a further 9% of the time it tells you where to put a couple of debug prints to pinpoint th trouble. For the other 1%, there are many Java experts on this site!
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 2,102
Reputation: BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold 
Solved Threads: 259
BestJewSinceJC BestJewSinceJC is offline Offline
Postaholic

Re: JComboBox help

 
0
  #5
Apr 10th, 2009
Also, the "Working as it is supposed to" is just a figment of your imagination. If it doesn't work the first time, it is just tricking you the third time. Hehe.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 387 | Replies: 4
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC