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);
        }
    }
}    
ZenenT
  • 45
  • 4
  • 1
    It's not very clear as to what you're asking. And you give no code to help. – AntonH May 05 '14 at 20:38
  • 1
    possible duplicate of [Assigning variables with dynamic names in Java](http://stackoverflow.com/questions/6729605/assigning-variables-with-dynamic-names-in-java) – user2864740 May 05 '14 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*](http://docs.oracle.com/javase/tutorial/collections/) to handle "dynamic variable names". – user2864740 May 05 '14 at 20:43
  • What do you mean by `illegal forward reference` error? Show enough code to reproduce the issue. – Kevin Panko May 06 '14 at 14:39

2 Answers2

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(...);
mellamokb
  • 56,094
  • 12
  • 110
  • 136
  • I would go with this suggestion :-) +1 – Afzaal Ahmad Zeeshan May 05 '14 at 20:39
  • 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? – ZenenT May 05 '14 at 21:13
  • 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. – mellamokb May 06 '14 at 12:46
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));
}
amit
  • 175,853
  • 27
  • 231
  • 333
  • Just curious, why is using reflection considered bad practice? I'm very new to Java, and coding as a whole. – ZenenT May 05 '14 at 21:17
  • @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. – amit May 05 '14 at 21:31