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.
JamesCherrill
Posting Genius
6,371 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
There is no condition on an "else" clause, so this isn't valid syntax
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.
Ezzaral
Posting Genius
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
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
for (int i=0; i<jComboBox1.getSelectedIndex(); i++) {
textFields[i].setVisible(true);
}
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.)
switch (jComboBox1.getSelectedIndex()){
case 2:
cTextField.setVisible(true);
// fall through
case 1:
bTextField.setVisible(true);
// fall through
case 0:
aTextField.setVisible(true);
}
Ezzaral
Posting Genius
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
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.
Ezzaral
Posting Genius
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
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.
Ezzaral
Posting Genius
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
Yes, that should be the case. If you're using netbeans to debug it, set a break point on the switch statement.
One note on your switch though. If you want to it to perform like the series of if() statements you had earlier, you need to remove the break statements and let the cases fall through so they are additive.
Ezzaral
Posting Genius
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
Since you need to set visible true or false for every field, maybe now is a good time to re-visit the array of fields solution
for (int i = 0; i < 10; i++) {
arrayOfFields[i].setVisible(i<= ComboBox1.getSelectedIndex());
}
JamesCherrill
Posting Genius
6,371 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
Hi. I'm suggesting the array/loop approach because if you have 10 of anything without them being in an array, you will end up with 10 almost identical blocks of code for everything you do.
You can just set them up in the array from scratch, then refer to them by array[i] rather than field1, field2 etc
JTextField[] arrayOfFields = new JTextField[10];
for (int i = 0; i < 10; i++) {
arrayOfFields[i] = new JTextField("");
form.add(arrayOfFields[i]);
arrayOfFields[i]. // etc etc
}
JamesCherrill
Posting Genius
6,371 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073