Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: How to make a more than one line JLabel or drawString for more than one line.

  1. #1
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Question How to make a more than one line JLabel or drawString for more than one line.

    How would someone make a JLabel be one more than one line, say for a test question or instruction or something, and how would one make the Graphics method drawString draw a String on more than one line?

    Is /n (or was it \n) good enough to do it?

    I think I tried that for JLabel and it wouldn't work.

    So what do I do?


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,408
    Thanks
    49
    Thanked 2,214 Times in 2,187 Posts

    Default Re: How to make a more than one line JLabel or drawString for more than one line.

    Look at adding HTML statements with <BR> to make the text be on more than one line.

  3. The Following User Says Thank You to Norm For This Useful Post:

    javapenguin (June 4th, 2011)

  4. #3
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: How to make a more than one line JLabel or drawString for more than one line.

    So what does

    g.drawString("<html> <BR>What does this do? </BR> <BR> Does it make two lines? </BR></html>", 1000,1000);

    do?

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,408
    Thanks
    49
    Thanked 2,214 Times in 2,187 Posts

    Default Re: How to make a more than one line JLabel or drawString for more than one line.

    What happens when you execute it? That will show you what it does.

  6. #5
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: How to make a more than one line JLabel or drawString for more than one line.

    It doesn't do what I thought it would do.

    It just draws the whole thing literally.

    So how do I use it then? I've tried it even with just one set of <br> </br> and it still draws it literally.

  7. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,408
    Thanks
    49
    Thanked 2,214 Times in 2,187 Posts

    Default Re: How to make a more than one line JLabel or drawString for more than one line.

    drawString is a primitive method that does exactly what you tell it to do. IE literally
    Its up to you to set the x,y values for each line to the location where you want it to draw.

  8. #7
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: How to make a more than one line JLabel or drawString for more than one line.

    Ok, it appears to work for JLabel but not for drawString().

    How do I get it to drawString() on more than one line without the added hassle of having to make two drawString() call to make it two lines?

    The <BR> thing wouldn't work for that one.

  9. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,408
    Thanks
    49
    Thanked 2,214 Times in 2,187 Posts

    Default Re: How to make a more than one line JLabel or drawString for more than one line.

    without the added hassle
    Sorry, that's the way drawString works.

  10. #9
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: How to make a more than one line JLabel or drawString for more than one line.

    How do people do it then?

    However, I can always repaint the JLabel though I suppose instead.

  11. #10
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: How to make a more than one line JLabel or drawString for more than one line.

    Ok, how do you change the color of a JLabel?

    Never mind, it's setForeground() that does that.

    Problem solved.
    Last edited by javapenguin; June 4th, 2011 at 02:18 PM.

Similar Threads

  1. Help with a line of my code.
    By Miical94 in forum Object Oriented Programming
    Replies: 1
    Last Post: March 7th, 2011, 10:00 AM
  2. [SOLVED] New line and StringBuilder
    By newbie in forum What's Wrong With My Code?
    Replies: 9
    Last Post: January 1st, 2011, 08:42 PM
  3. Getting all points in line
    By Mike in forum Java Theory & Questions
    Replies: 7
    Last Post: September 13th, 2010, 11:59 AM
  4. Reading a file line by line using the Scanner class
    By JavaPF in forum File Input/Output Tutorials
    Replies: 0
    Last Post: April 17th, 2009, 07:34 AM
  5. How to Read a file line by line using BufferedReader?
    By JavaPF in forum File Input/Output Tutorials
    Replies: 0
    Last Post: May 19th, 2008, 06:32 AM