This week's book giveaway is in the JavaScript forum.
We're giving away four copies of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js and have Paul Jensen on-line!
See this thread for details.
Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Getting key labels to show in textfield  RSS feed

 
Tesla Doty
Greenhorn
Posts: 10
Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am working on a keypad for a phone app for school. I cannot get the numbers from the keys to show up in the textField. I have made multiple tweaks, but think maybe my case statement is the problem? We did a calculator application in the class, so I have based my code off of that. Although the telephone keypad seemed much simpler to code, I can't seem to get the numbers to show up. Any help would be appreciated.

P.S. I am SO happy to have found a forum where I can ask noob questions and get feedback without senior devs thinking I am a complete idiot!

>
 
Daryl Cofer
Greenhorn
Posts: 19
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you need a switch statement there?
Switch statements are like an if-else statement. In this instance, case 0, 1, 2, 3, 4...12 are all calling the same setText method. You could do away with the switch completely and just have


Which simply says that if we found the key, set the text of the lcd to the key's label.
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch


So what exactly is the source? The individual key? or the array of keys?
 
Tesla Doty
Greenhorn
Posts: 10
Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Daryl Cofer, thank you, that does make more sense than what I was doing. I have been referring to a calculator app tutorial we did in the class. When I couldn't get the numbers to show in the lcd, I thought maybe that was missing. However, after reading up on cases I realize that didn't make sense there.

Maneesh Godbole, I would want to getSource from the key that was clicked, not the array. I was thinking maybe getting the key that was clicked and assigning a variable with that value, but not sure how to code that.



Something like this, only that is syntactically correct. Thanks again for the help, guys!
 
Campbell Ritchie
Sheriff
Posts: 55333
157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I personally think that using sources like that for a calculator is pretty dreadful design. Whoever writes tutorials with that sort of thing in should be tied down with NullPointerExceptions and have action events shoved under their fingernails. You end up with dreadful banks of if-elses like this:-Now, if you had a real object‑oriented solution it would be something likeIf you find out about using Actions, they are like ActionListeners but probably better. In Java8 there may be a simpler solution with the -> operator.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!