Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JTextField and passing ints

 
Chris Gohlinghorst
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As I understand it, JTextField only takes Strings. However, I need to ouput an int to it. I can think of some convoluted ways to do that, but I'm assuming there is an easier way to go about this that I'm clearly missing.

Any suggestions where I can look to find this answer or is there some already cooked code people use to make a quick conversion?

Any help would be most appreciated,

Chris
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34095
337
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chris,
Welcome to JavaRanch!

How about just doing ""+ int to create a String?
 
Campbell Ritchie
Sheriff
Posts: 48445
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Integer.toString(i)?
String.format("%d", i)?

You will probably find Joanne's answer the simplest.
[edit]Change mistaken % to "[/edit]
[ November 22, 2008: Message edited by: Campbell Ritchie ]
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know why, but this makes me cringe for some reason:

I don't think it's a kluge but just doesn't seem fully kosher, and again, I'm not sure why I feel this way.

I usually do (and this is the fourth way demonstrated in this thread, I think):

 
Rob Spoor
Sheriff
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd use String.valueOf(myInt) myself, but that does call Integer.toString(myInt, 10). Which in turn calls Integer.toString(myInt) (radix 10 is a special case).

So if we want to use the most efficient one, then I think Integer.toString(myInt) will be the best.
 
Andre Uhres
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, we may want to use a NumericTextField. Our parsing is then guaranteed to succeed.
EDIT: oups, it seems you use the field for output only!
[ November 23, 2008: Message edited by: Andre Uhres ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic