• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JTextField and passing ints

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Chris,
Welcome to JavaRanch!

How about just doing ""+ int to create a String?
 
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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):

 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
reply
    Bookmark Topic Watch Topic
  • New Topic