Results 1 to 3 of 3

Thread: Swing question

  1. #1
    BenH is offline Member
    Join Date
    Dec 2011
    Posts
    48
    Rep Power
    0

    Default 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!

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,234
    Rep Power
    7

    Default 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:
    Java Code:
    String variable = "some text";
    label.setText(variable);
    If you need more help then post your Short, Self Contained, Correct Example that demonstrates the problem.

  3. #3
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default 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:

    Java Code:
    int myInt = 42;
    JLabel label = new JLabel(Integer.toString(myInt));
    There's also a lazy and inefficient way of doing it:

    Java Code:
    int myInt = 42;
    JLabel label = new JLabel("" + myInt);
    Get in the habit of using standard Java naming conventions!

Similar Threads

  1. JDBC with Swing question
    By hacker9116 in forum JDBC
    Replies: 4
    Last Post: 04-22-2011, 05:03 PM
  2. Swing timers question?
    By intel i7 in forum AWT / Swing
    Replies: 5
    Last Post: 12-25-2010, 12:39 AM
  3. Swing question
    By Psyclone in forum New To Java
    Replies: 0
    Last Post: 02-02-2010, 10:50 PM
  4. Question on swing timers
    By Samgetsmoney in forum New To Java
    Replies: 5
    Last Post: 02-20-2009, 07:34 AM
  5. Swing Question, can you help?
    By Silentstormz in forum AWT / Swing
    Replies: 4
    Last Post: 09-21-2008, 03:02 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •