aspose file tools*
The moose likes Beginning Java and the fly likes JTextField and passing ints Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "JTextField and passing ints" Watch "JTextField and passing ints" New topic
Author

JTextField and passing ints

Chris Gohlinghorst
Greenhorn

Joined: Nov 22, 2008
Posts: 1
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

Joined: May 26, 2003
Posts: 30929
    
158

Chris,
Welcome to JavaRanch!

How about just doing ""+ int to create a String?


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39828
    
  28
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

Joined: Feb 23, 2007
Posts: 1561
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

Joined: Oct 27, 2005
Posts: 19760
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Andre Uhres
Greenhorn

Joined: Nov 23, 2008
Posts: 23
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 ]
 
 
subject: JTextField and passing ints