0

So I have a series of jButtons named card1 to card20. I want to change the icon based on whether or not a specific condition has been fulfilled, so I'd like to make a loop and refer to each one as ("card" + i) or something similar instead of writing separate if statements for each button. The code I'm using has been added below, but is there a way to loop this if statement so each loop of the code affects a different card?

private void cardreset() {
    if (cardmatch[1] == 0) {
        card1.setIcon(back);
    }
}

This is what I'd like to do, but adding all of the "card" variables to an array beforehand creates an illegal forward reference error.

private void cardreset() {
    for(int i=1; i<=20; i++){
        if (cardmatch[i] == 0) {
            card[i].setIcon(back);
        }
    }
}    
4
  • 1
    It's not very clear as to what you're asking. And you give no code to help. Commented May 5, 2014 at 20:38
  • 1
    possible duplicate of Assigning variables with dynamic names in Java Commented May 5, 2014 at 20:40
  • You could even rely on the ordering of JButton controls in the container (which would then act like an array), if other assumptions are made, instead of creating a separate array. But it's the same idea: using a collection to handle "dynamic variable names". Commented May 5, 2014 at 20:43
  • What do you mean by illegal forward reference error? Show enough code to reproduce the issue. Commented May 6, 2014 at 14:39

2 Answers 2

2

You can put them in an array and modify them that way.

JButton cards[] = { card1, card2, ..., card20 };

Then when you want to modify all the icons:

if (condition) {

  for (JButton card : cards)
    card.setIcon(...);

}

Or modify specific icons (say every other one):

for (int i = 0; i < cards.length; i++)
  if (i % 2 == 0)
    cards[i].setIcon(...);
Sign up to request clarification or add additional context in comments.

3 Comments

I would go with this suggestion :-) +1
Okay, I'm trying to do this but I'm getting an "illegal forward reference" error for each one. I think that it has to do with the button variables being declared later on in the program. Any suggestions?
Yes, you will want to declare it as JButton cards[] = new JButton[20], then set them later in the program as card[0] = new JButton(...), and card[1] = new JButton(...), etc. in place of card0 = ..., etc.
1

Theoretically you could do it with reflection - but that's a really bad practice.

Instead, you should use a Map<String, Type>, and use your map to refer them.

Just for the fun of it, here how to do it with reflection, but again, I strongly advise against it.

for (int i = 0; i < 4; i ++) { 
    Field f = MyClass.class.getDeclaredField("card" + i);
    System.out.println(f.get(myClass));
}

2 Comments

Just curious, why is using reflection considered bad practice? I'm very new to Java, and coding as a whole.
@ZenenT Many reasons. First, there is no checking during compilation at all, and if you rename your variable and forget to change the string - you'll get a run time exception, which is always worse than compilation error. On top of it, using reflection is slower than using the "normal" control flow, and usually prevent the java virtual machine (jvm) from doing some optimizations on run time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.