This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
I wish to create new variables from a single variable. Let me explain. I am creating a hangman game. I have a Button in my program called alphbutt. And I have a String array in my program called alpharray. Alpharray, as you may have guessed, holds letters from A-Z. Alphbutt is simply a blank button. Now, I wish to create an Alphbutt1, Alphbutt2, Alphbutt3, etc. without declaring these new Alphbutt(s). The reason is so that each button is a unique variable. Let me clarify myself further. This is a small portion of my code.
As you can see, for every button I have initialized, I have added an ActionListener so that it goes to the actionPerformed method. However, if you recall, alphbutt is only equal to the final variable I have initialized, and that is Z. Therefore, whenever I enter the actionPerformed method, the method only executes for the value of Z, and for none other button. If I click on button M, for example, I enter the actionPerformed method, but since alphbutt is equal to Z, it only performs for Z, even though I want it to perform for M or any other letter I have in mind. This is why I want to make different variables from a single variable using a for loop. I want letter A to be alphbutt0, B to be alphbutt1, just any distinction between each button value.
If you are still confused, please ask me. Thank you for any and all replies.
It sounds to me like an issue with your ActionListener. Can you post the implementation of your actionPerformed() method?
One thing you can have a look at, just off the top of my head, is assigning each new button you create an "action command." That could be it's letter, for example. In your actionPerformed(), you could retrieve this String and do something with it.