jComboBox getSelectedIndex

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Apr 2009
Posts: 114
Reputation: KirkPatrick is an unknown quantity at this point 
Solved Threads: 3
KirkPatrick KirkPatrick is offline Offline
Junior Poster

jComboBox getSelectedIndex

 
0
  #1
May 18th, 2009
Just had a quick question about my code. Seems like a very simple solution, perhaps I'm just too sleepy to think straight.

What I am wanting to do is if a certain index is selected in my combobox i want certain textfields to display which is apparent by the code:

  1. private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {
  2. if (jComboBox1.getSelectedIndex == 0) {
  3. aTextField.setVisible(true);
  4. }
  5. else if (jComboBox1.getSelectedIndex == 1) {
  6. aTextField.setVisible(true);
  7. bTextField.setVisible(true);
  8. }
  9. else if (jComboBox1.getSelectedIndex == 2) {
  10. aTextField.setVisible(true);
  11. bTextField.setVisible(true);
  12. cTextField.setVisible(true);
  13. }
  14. else if (jComboBox1.getSelectedIndex == 3) {
  15. aTextField.setVisible(true);
  16. bTextField.setVisible(true);
  17. cTextField.setVisible(true);
  18. dTextField.setVisible(true);
  19. }
  20. else if (jComboBox1.getSelectedIndex == 4) {
  21. aTextField.setVisible(true);
  22. bTextField.setVisible(true);
  23. cTextField.setVisible(true);
  24. dTextField.setVisible(true);
  25. eTextField.setVisible(true);
  26. }
  27. else if (jComboBox1.getSelectedIndex == 5) {
  28. aTextField.setVisible(true);
  29. bTextField.setVisible(true);
  30. cTextField.setVisible(true);
  31. dTextField.setVisible(true);
  32. eTextField.setVisible(true);
  33. fTextField.setVisible(true);
  34. }
  35. else if (jComboBox1.getSelectedIndex == 6) {
  36. aTextField.setVisible(true);
  37. bTextField.setVisible(true);
  38. cTextField.setVisible(true);
  39. dTextField.setVisible(true);
  40. eTextField.setVisible(true);
  41. fTextField.setVisible(true);
  42. gTextField.setVisible(true);
  43. }
  44. else if (jComboBox1.getSelectedIndex == 7) {
  45. aTextField.setVisible(true);
  46. bTextField.setVisible(true);
  47. cTextField.setVisible(true);
  48. dTextField.setVisible(true);
  49. eTextField.setVisible(true);
  50. fTextField.setVisible(true);
  51. gTextField.setVisible(true);
  52. hTextField.setVisible(true);
  53. }
  54. else if (jComboBox1.getSelectedIndex == 8) {
  55. aTextField.setVisible(true);
  56. bTextField.setVisible(true);
  57. cTextField.setVisible(true);
  58. dTextField.setVisible(true);
  59. eTextField.setVisible(true);
  60. fTextField.setVisible(true);
  61. gTextField.setVisible(true);
  62. hTextField.setVisible(true);
  63. iTextField.setVisible(true);
  64. }
  65. else (jComboBox1.getSelectedIndex == 9) {
  66. aTextField.setVisible(true);
  67. bTextField.setVisible(true);
  68. cTextField.setVisible(true);
  69. dTextField.setVisible(true);
  70. eTextField.setVisible(true);
  71. fTextField.setVisible(true);
  72. gTextField.setVisible(true);
  73. hTextField.setVisible(true);
  74. iTextField.setVisible(true);
  75. jTextField.setVisible(true);
  76. }
  77. }

Perhaps I'm just not paying attention close enough, but shouldn't that code work?
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 983
Reputation: JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice 
Solved Threads: 145
JamesCherrill JamesCherrill is online now Online
Posting Shark

Re: jComboBox getSelectedIndex

 
0
  #2
May 18th, 2009
If your code is an ActionL:istener, you're probably listening for the wrong event to see if the selection has changed. You probably need an ItemListener.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,490
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 517
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: jComboBox getSelectedIndex

 
0
  #3
May 18th, 2009
There is no condition on an "else" clause, so this isn't valid syntax
  1. else (jComboBox1.getSelectedIndex == 9)
.

The bigger question is why not use a switch or even better yet, an array of text fields so you don't have so much manually repetitive code.
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 114
Reputation: KirkPatrick is an unknown quantity at this point 
Solved Threads: 3
KirkPatrick KirkPatrick is offline Offline
Junior Poster

Re: jComboBox getSelectedIndex

 
0
  #4
May 18th, 2009
Originally Posted by JameCherrill
If your code is an ActionL:istener, you're probably listening for the wrong event to see if the selection has changed. You probably need an ItemListener.
I went ahead and tried it for my itemListener with the same luck. This may be a solution to a question later in the situation, but for right now its some sort of syntax error. (its supposed to be .getSelectedIndex(int) but that didn't work which is why I tried .getSelectedIndex == 0)

I apologize as I thought I mentioned that it is a syntax error, and that it doesn't actually compile. I thought I had typed that, but looking back on my post, I didn't.

Originally Posted by Ezzaral
There is no condition on an "else" clause, so this isn't valid syntax
Indeed, was typing on a whim. That part has been fixed now.

The bigger question is why not use a switch or even better yet, an array of text fields so you don't have so much manually repetitive code.
I was typing it quickly to have an example as it seemed like a logical way to do it. Also if I'm not mistaken a switch statement would be nearly as many lines of code. As for an array of text fields, all in all it would still be just about as many lines of code but it would look cleaner to do it that way.

I'm not sure why I'm off on this one. I think I'm aiming a bit too much toward javascript instead of java
Last edited by KirkPatrick; May 18th, 2009 at 4:01 pm.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,490
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 517
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: jComboBox getSelectedIndex

 
1
  #5
May 18th, 2009
As for an array of text fields, all in all it would still be just about as many lines of code but it would look cleaner to do it that way.
Well, I would say this is a few less lines
  1. for (int i=0; i<jComboBox1.getSelectedIndex(); i++) {
  2. textFields[i].setVisible(true);
  3. }
A fall-through switch is more hard-coding than I would prefer, but still far fewer than that if-else block (which could really be just a series of if() statements to eliminate the repetitive statements.)
  1. switch (jComboBox1.getSelectedIndex()){
  2. case 2:
  3. cTextField.setVisible(true);
  4. // fall through
  5. case 1:
  6. bTextField.setVisible(true);
  7. // fall through
  8. case 0:
  9. aTextField.setVisible(true);
  10. }
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 114
Reputation: KirkPatrick is an unknown quantity at this point 
Solved Threads: 3
KirkPatrick KirkPatrick is offline Offline
Junior Poster

Re: jComboBox getSelectedIndex

 
0
  #6
May 18th, 2009
Oh wow, I was thinking of it in a completely different way for the array and for the switch statement. I retract my previous statement as you have proven me wrong. Thanks for showing me that, could come in handy.

Any suggestions as to why its saying I can't set it up with my .getSelectedIndex() ?
Last edited by KirkPatrick; May 18th, 2009 at 4:42 pm.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,490
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 517
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: jComboBox getSelectedIndex

 
0
  #7
May 18th, 2009
Originally Posted by KirkPatrick View Post
Any suggestions as to why its saying I can't set it up with my .getSelectedIndex() ?
The only problems I really saw with the if() block code you posted up above was the lack of parenthesis for the "getSelectedIndex" method calls and the condition statement on the else portion, which I mentioned previously. If neither of those are the problem, you'll need to post the exact error message and stack trace.
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 114
Reputation: KirkPatrick is an unknown quantity at this point 
Solved Threads: 3
KirkPatrick KirkPatrick is offline Offline
Junior Poster

Re: jComboBox getSelectedIndex

 
0
  #8
May 18th, 2009
I added that parenthesis and still had the same problem, no worries I just went ahead and used the method you showed me. Thanks againfor the help
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 114
Reputation: KirkPatrick is an unknown quantity at this point 
Solved Threads: 3
KirkPatrick KirkPatrick is offline Offline
Junior Poster

Re: jComboBox getSelectedIndex

 
0
  #9
May 20th, 2009
Just making a post to note that I unchecked it from being solved (sorry for double post)

I just now got to implementing the switch statement and its not working either :/

Here is what I've done...

I set all my textfields to visibility false and then put in the switch statement in both the ActionPerformed and ItemStateChange and for some reason it just keeps my textsfields set to false.

It compiles and everything fine, just doesn't perform the action.

Oh and just an update as to my original question of the syntax not working (in my first post) it was because my syntax, just a stupid mistake. This fixes the error for the first one, however like the switch statement has no effect when something is selected in the combobox

  1. if (jComboBox1.getSelectedIndex() == 0) {
  2. aTextField.setVisible(true);
  3. }

Any ideas?
Last edited by KirkPatrick; May 20th, 2009 at 12:02 pm.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,490
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 517
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: jComboBox getSelectedIndex

 
0
  #10
May 20th, 2009
Are you certain that you have added the listeners to the jComboBox1 control? You would need to post the full code to track it down further.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC