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:

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?

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.

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.

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.

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

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);
}
Comments
Thank you for further explaining that for me. To top it off, in a kind manner which not too many people do these days. Cheers bud

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() ?

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.

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

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

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

Any ideas?

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.

Well I'm using netbeans ide so I figured actionPerformed took care of the listeners with the ActionEvent evt

public myApp() {
        initComponents();
        
        aTextField.setVisible(false);
        bTextField.setVisible(false);
        cTextField.setVisible(false);
        dTextField.setVisible(false);
        eTextField.setVisible(false);
        fTextField.setVisible(false);
        gTextField.setVisible(false);
        hTextField.setVisible(false);
        iTextField.setVisible(false);
        jTextField.setVisible(false);
        
    setTitle("MyApp");

    }
private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {                                           
    
    switch (jComboBox1.getSelectedIndex()){
        case 9:
            jTextField.setVisible(true);
            break;
        case 8:
            iTextField.setVisible(true);
            break;
        case 7:
            hTextField.setVisible(true);
            break;
        case 6:
            gTextField.setVisible(true);
            break;
        case 5:
            fTextField.setVisible(true);
            break;
        case 4:
            eTextField.setVisible(true);
            break;
        case 3:
            dTextField.setVisible(true);
            break;
        case 2:
            cTextField.setVisible(true);
            break;
        case 1:
            bTextField.setVisible(true);
            break;
        case 0:
            aTextField.setVisible(true);
            break;
}

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.

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());
}

ah yes, that would make sense since I want it to continue going through each case below it without stopping. Thanks for informing me bud.

Anyways I think my problem was setting all my textfields to true above my switch statement. For some reason that was throwing it off. I might have to override it when doing a switch.

On a side note I went ahead and test some things out. (After going through it and put some print statements in on the switch statement which proved it to be working just not being able to over ride the setVisible(true) above it.

I did run into a problem though as I was testing it with if else statements. Since the textfields were automatically set true I had a problem with just trying to set certain ones true or false and had to make it specific for each text field like so:

if (jComboBox1.getSelectedIndex() == 0) {
        Dcc1TextField.setVisible(true);
        Dcc2TextField.setVisible(false);
        Dcc3TextField.setVisible(false);
        Dcc4TextField.setVisible(false);
        Dcc5TextField.setVisible(false);
        Dcc6TextField.setVisible(false);
        Dcc7TextField.setVisible(false);
        Dcc8TextField.setVisible(false);
        Dcc9TextField.setVisible(false);
        Dcc10TextField.setVisible(false);   
    }
    else if (jComboBox1.getSelectedIndex() == 1) {
        Dcc1TextField.setVisible(true);
        Dcc2TextField.setVisible(true);
        Dcc3TextField.setVisible(false);
        Dcc4TextField.setVisible(false);
        Dcc5TextField.setVisible(false);
        Dcc6TextField.setVisible(false);
        Dcc7TextField.setVisible(false);
        Dcc8TextField.setVisible(false);
        Dcc9TextField.setVisible(false);
        Dcc10TextField.setVisible(false); 
    }
...and so on

When trying to do it by just telling which ones to setVisible(true) or setVisible(false) it occasionally wouldn't update the GUI.

Using the switch statement also caused problems with the GUI. For example when index 9 was chosen it would display 10 textfields, then index 4 was chosen and it would show 5 textfields, and then choosing index 9 again instead of showing 10 textfields it would stay with 5 textfields.

So basically I figured out why it wasn't displaying them, but now I am intrigued with the switch statement you have shown me and I guess I was wondering if there is a way to get it working with the way my if else statement now works?

Thanks again for your continuous help bud


*Edit*

Just noticed that JamesCherrill has also replied. So would this array of fields also solve the problem of certain textfields not showing up? Judging from the looks of it, it should. However I have a question as I get confused with loops. For the arrayOfFields, I assume I would have to make an array which contains each field name in sequential order?

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
Help with Code Tags
java Syntax (Toggle Plain Text)

for (int i = 0; i < 10; i++) {
      arrayOfFields[i].setVisible(i<= ComboBox1.getSelectedIndex());
   
      }

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 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
}
This question has already been answered. Start a new discussion instead.