| | |
jComboBox getSelectedIndex
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Apr 2009
Posts: 114
Reputation:
Solved Threads: 3
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:
Perhaps I'm just not paying attention close enough, but shouldn't that code work?
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:
Java Syntax (Toggle Plain Text)
private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) { if (jComboBox1.getSelectedIndex == 0) { aTextField.setVisible(true); } else if (jComboBox1.getSelectedIndex == 1) { aTextField.setVisible(true); bTextField.setVisible(true); } else if (jComboBox1.getSelectedIndex == 2) { aTextField.setVisible(true); bTextField.setVisible(true); cTextField.setVisible(true); } else if (jComboBox1.getSelectedIndex == 3) { aTextField.setVisible(true); bTextField.setVisible(true); cTextField.setVisible(true); dTextField.setVisible(true); } else if (jComboBox1.getSelectedIndex == 4) { aTextField.setVisible(true); bTextField.setVisible(true); cTextField.setVisible(true); dTextField.setVisible(true); eTextField.setVisible(true); } else if (jComboBox1.getSelectedIndex == 5) { aTextField.setVisible(true); bTextField.setVisible(true); cTextField.setVisible(true); dTextField.setVisible(true); eTextField.setVisible(true); fTextField.setVisible(true); } else if (jComboBox1.getSelectedIndex == 6) { aTextField.setVisible(true); bTextField.setVisible(true); cTextField.setVisible(true); dTextField.setVisible(true); eTextField.setVisible(true); fTextField.setVisible(true); gTextField.setVisible(true); } else if (jComboBox1.getSelectedIndex == 7) { aTextField.setVisible(true); bTextField.setVisible(true); cTextField.setVisible(true); dTextField.setVisible(true); eTextField.setVisible(true); fTextField.setVisible(true); gTextField.setVisible(true); hTextField.setVisible(true); } else if (jComboBox1.getSelectedIndex == 8) { aTextField.setVisible(true); bTextField.setVisible(true); cTextField.setVisible(true); dTextField.setVisible(true); eTextField.setVisible(true); fTextField.setVisible(true); gTextField.setVisible(true); hTextField.setVisible(true); iTextField.setVisible(true); } else (jComboBox1.getSelectedIndex == 9) { aTextField.setVisible(true); bTextField.setVisible(true); cTextField.setVisible(true); dTextField.setVisible(true); eTextField.setVisible(true); fTextField.setVisible(true); gTextField.setVisible(true); hTextField.setVisible(true); iTextField.setVisible(true); jTextField.setVisible(true); } }
Perhaps I'm just not paying attention close enough, but shouldn't that code work?
There is no condition on an "else" clause, so this isn't valid syntax .
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.
Java Syntax (Toggle Plain Text)
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.
•
•
Join Date: Apr 2009
Posts: 114
Reputation:
Solved Threads: 3
•
•
•
•
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 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
•
•
•
•
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'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.
•
•
•
•
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.
java Syntax (Toggle Plain Text)
for (int i=0; i<jComboBox1.getSelectedIndex(); i++) { textFields[i].setVisible(true); }
java Syntax (Toggle Plain Text)
switch (jComboBox1.getSelectedIndex()){ case 2: cTextField.setVisible(true); // fall through case 1: bTextField.setVisible(true); // fall through case 0: aTextField.setVisible(true); }
•
•
Join Date: Apr 2009
Posts: 114
Reputation:
Solved Threads: 3
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() ?
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.
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.
•
•
Join Date: Apr 2009
Posts: 114
Reputation:
Solved Threads: 3
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
Any ideas?
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
Java Syntax (Toggle Plain Text)
if (jComboBox1.getSelectedIndex() == 0) { aTextField.setVisible(true); }
Any ideas?
Last edited by KirkPatrick; May 20th, 2009 at 12:02 pm.
![]() |
Similar Threads
- JComboBox help (Java)
- JComboBox (Java)
- I lack focus... (Java)
- Display Value in JcomboBox (Java)
- JComboBox -- Item is not in the list???????? (Java)
- Help with a reservation program! GUI Messed XD (Java)
- Want to use radio button or menu bar instead of combo box (Java)
Other Threads in the Java Forum
- Previous Thread: Truble with tim.start();
- Next Thread: Automatic search.
| Thread Tools | Search this Thread |
-xlint actionlistener android api applet application array arrays automation binary blackberry block bluetooth character chat class classes client code component consumer database desktop developmenthelp draw eclipse error event exception fractal ftp game givemetehcodez graphics gui html ide image input integer j2me j2seprojects java javac javaee javaprojects jmf jni jpanel julia lego linked linux list loop loops mac map method methods mobile netbeans newbie notdisplaying number online oracle page print printf problem program programming project properties recursion researchinmotion rotatetext rsa scanner screen server set singleton size sms sort sql string swing template textfields threads time title tree tutorial-sample update windows working






