• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Create new variables from a single variable

 
S. Hoysala
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,

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.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12084
29
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mostly, I am confused as to why this was posted in a forum named "Meaningless Drivel". Generally, questions about how to write code go in our forums devoted to, well, writing code.

So, I'll move it for you.
 
S. Hoysala
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, Fred, for doing that for me.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One approach would be to create an array of Buttons...

Then instead of Alphbutt1, Alphbutt2, etc., you would have alphaButton[0], alphaButton[1], etc.
 
Jason Bullers
Ranch Hand
Posts: 111
8
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic