Results 1 to 3 of 3
Thread: Swing question
- 02-11-2012, 05:08 AM #1
Member
- Join Date
- Dec 2011
- Posts
- 48
- Rep Power
- 0
Swing question
Hi there.
I am currently learning swing. My next program that I am supposed to design is to use a GUI. I know how to do that. How do I display text that includes variables? I tried JLabel, but I get an error which basically states I cannot use a variable with JLabel. It has to be a string. Is there any other way of doing this? Thanks!
- 02-11-2012, 05:17 AM #2
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
Re: Swing question
> I cannot use a variable with JLabel. It has to be a string.
I don't understand your question. You can do:
If you need more help then post your Short, Self Contained, Correct Example that demonstrates the problem.Java Code:String variable = "some text"; label.setText(variable);
- 02-11-2012, 06:12 AM #3
Re: Swing question
Objects have a toString() method. Primitive types (int, etc.) can be converted using the static toString() method of their wrapper class. For example:
There's also a lazy and inefficient way of doing it:Java Code:int myInt = 42; JLabel label = new JLabel(Integer.toString(myInt));
Java Code:int myInt = 42; JLabel label = new JLabel("" + myInt);Get in the habit of using standard Java naming conventions!
Similar Threads
-
JDBC with Swing question
By hacker9116 in forum JDBCReplies: 4Last Post: 04-22-2011, 05:03 PM -
Swing timers question?
By intel i7 in forum AWT / SwingReplies: 5Last Post: 12-25-2010, 12:39 AM -
Swing question
By Psyclone in forum New To JavaReplies: 0Last Post: 02-02-2010, 10:50 PM -
Question on swing timers
By Samgetsmoney in forum New To JavaReplies: 5Last Post: 02-20-2009, 07:34 AM -
Swing Question, can you help?
By Silentstormz in forum AWT / SwingReplies: 4Last Post: 09-21-2008, 03:02 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks