Results 1 to 12 of 12
Thread: Print int in a JLabel
- 02-18-2013, 03:13 PM #1
Senior Member
- Join Date
- Jul 2012
- Posts
- 165
- Rep Power
- 1
Print int in a JLabel
I have a java file containing many methods which do calculations based on some variables and arrays in my program to give ints as a result. I now want to be able to show this data to my users by putting it in a JLabel. However, as the output is int instead of String, I don't know how to do this.
Example code:
Where I am trying to use it:
The method returning an int:Java Code:JLabel totalSeatsTheatreAnswerLabel = new JLabel(Statistics.totalSeatsTheatre());
Java Code:public static int totalSeatsTheatre(){ return (BookingsSystem.A.length + BookingsSystem.B.length + BookingsSystem.C.length + BookingsSystem.D.length + BookingsSystem.E.length + BookingsSystem.F.length + BookingsSystem.G.length + BookingsSystem.H.length + BookingsSystem.J.length + BookingsSystem.K.length + BookingsSystem.L.length); }
- 02-18-2013, 03:19 PM #2
Re: Print int in a JLabel
Check out the API for both the String and Integer class for useful functions.
How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 02-18-2013, 03:25 PM #3
Godlike
- Join Date
- Nov 2012
- Posts
- 194
- Rep Power
- 1
Re: Print int in a JLabel
JLabel expects a String as parameter. Method totalSeatsTheatrs returns an int, and constructor JLabel(int) does not exist. There are several options KevinWorkman is pointing to:
The official way: new JLabel(String.valueOf(totalSeatsTheatre()));
or: new JLabel(Integer.toString(totalSeatsTheatre()));
or The Quick But Ugly Way™: new JLabel("" + totalSeatsTheatre());Last edited by SurfMan; 02-18-2013 at 03:27 PM. Reason: Added ()
- 02-18-2013, 03:40 PM #4
Re: Print int in a JLabel
Note that String.valueOf(int) simply calls Integer.toString(int). The quick but ugly way you mention should not be used.
How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 02-18-2013, 03:43 PM #5
Senior Member
- Join Date
- Jul 2012
- Posts
- 165
- Rep Power
- 1
Re: Print int in a JLabel
ah, those seem perfect, thanks.
I will probably use toString but why is the last method ugly?
- 02-18-2013, 03:47 PM #6
Re: Print int in a JLabel
The last method of using String concatenation to combine an empty String with your int is ugly because it's doing unnecessary work. Behind the scenes, the append operator is creating a StringBuilder (or StringBuffer) instance, then appending the two operands, then calling toString(). So if you do this in a loop, for example, it can kill performance in a way that isn't always obvious to track down. It's best not to get into the habit in the first place.
To put it another way, if you had an interview for a job and used "the ugly way" to turn an int into a String, you wouldn't get the job.How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 02-18-2013, 03:53 PM #7
Godlike
- Join Date
- Nov 2012
- Posts
- 194
- Rep Power
- 1
Re: Print int in a JLabel
My preferred way is String.valueOf(...) since you can use that for all primitives, so if you decide your int needs to be a long, you don't have to change all the toString() calls. But that's a matter of preference.
Fun fact: Integer.toString() calls String.valueOf(int) which in turn calls Integer.toString(int, 10)... Somebody didn't read their own API :)Last edited by SurfMan; 02-18-2013 at 03:55 PM.
- 02-18-2013, 03:53 PM #8
Senior Member
- Join Date
- Jul 2012
- Posts
- 165
- Rep Power
- 1
- 02-18-2013, 04:10 PM #9
Re: Print int in a JLabel
That's fair. The potential downside of that is it'll be less obvious if you pass in a wrong type. That'll be a runtime error (or worse, a logic error that leads to strange behavior) instead of a compile-time error. Like you said, it's a matter of preference and context.
Actually I looked directly at the code for Integer.toString(int), which does not call Integer.toString(int, 10) or String.valueOf(). The API mentions Integer.toString(int, 10) as an example of the value returned, but that doesn't mean it actually calls the method. Even if it does, I'm not sure how that negates me pointing out that String.valueOf() just calls the Integer.toString() method.
Java Code:public static String toString(int i) { if (i == Integer.MIN_VALUE) return "-2147483648"; int size = (i < 0) ? stringSize(-i) + 1 : stringSize(i); char[] buf = new char[size]; getChars(i, size, buf); return new String(buf, true); }Last edited by KevinWorkman; 02-18-2013 at 04:13 PM.
How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 02-18-2013, 04:12 PM #10
Re: Print int in a JLabel
How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 02-18-2013, 06:13 PM #11
Godlike
- Join Date
- Nov 2012
- Posts
- 194
- Rep Power
- 1
Re: Print int in a JLabel
I didn't negate any of your points. I am with you here. I am on your side. Just adding to the conversation. I merely looked into the source code to see how the different utility methods worked and ran into the little gem I mentioned earlier. Coming to that, that is actually JDK 6 source. I opened a random project I have in IntelliJ IDEA and examined the source and apparently it still ran on JDK 6. Didn't check. Your example is from JDK 7 so that makes it two valid points :) Nitpick: I pointed out something about Integer.toString() and not Integer.toString(int).
Anyway, I think the OP got the point :)
- 02-18-2013, 06:25 PM #12
Moderator
- Join Date
- Apr 2009
- Posts
- 10,467
- Rep Power
- 16
Re: Print int in a JLabel
Single point of change should they decide to change the default radix of toStringing() and int.
String contains all the standard toString() methods for primitives, so that was the obvious place to put that default.
Consequently the toString for the primitive wrappers uses those base methods.
I can see the logic, put it that way.Please do not ask for code as refusal often offends.
Similar Threads
-
I want to print two variables in same System.out.print, but not as one sum
By LasseA in forum New To JavaReplies: 6Last Post: 01-07-2013, 02:48 PM -
Print JLabel with a button ?
By garnachito in forum New To JavaReplies: 2Last Post: 05-28-2012, 04:41 AM -
Javax Print Attribute for Selection Print Range
By rsawatzky in forum AWT / SwingReplies: 0Last Post: 04-26-2012, 12:14 AM -
Adding a JLabel to a JPanel - jlabel not showing
By Bongeh in forum New To JavaReplies: 17Last Post: 04-06-2010, 11:02 PM -
Print the text file and print preview them
By Java Tip in forum java.awtReplies: 0Last Post: 06-22-2008, 11:04 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks