Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 5854
  • Last Modified:

JLabel - automatic word-wrap

Hi.

How can I make a JLabel automatically write text on multiple lines (if it doesn't fit in a single line)?

Thanks
0
slavikn
Asked:
slavikn
  • 6
  • 4
  • 4
  • +1
1 Solution
 
riaancorneliusCommented:
surround your text with <html> and </html> tags.
like <html>yourString</html>
0
 
girionisCommented:
You have to use HTML

new JLabel("<html><body>hey<br>and another hey<br>hey hey.");
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
girionisCommented:
typo

new JLabel("<html><body>hey<br>and another hey<br>hey hey.</body></hmtl>");
0
 
CEHJCommented:
html is *not* automatic ;-)
0
 
riaancorneliusCommented:
btw, another typo...
new JLabel("<html><body>hey<br>and another hey<br>hey hey.</body></html>");

you had </hmtl>... I checked because I always do that...

Guess it's a matter of opinion CEHJ. With html , you need to change every String in the app, with your link, you need to change every JLabel in the app :) There is bo such thing as an automatic solution.
0
 
slaviknAuthor Commented:
Thanks for the quick replies. I use (need the text centered):
<html><center>Baltic Avenue</center></html>.
0
 
riaancorneliusCommented:
thanks slavikn.
0
 
CEHJCommented:
>>Thanks for the quick replies. I use (need the text centered):

Where does the word wrap come in then?
0
 
CEHJCommented:
But quite apart from that, the accepted answer does *not* answer the given question (no offence i hope riaancornelius ;-)) so i'm wondering why you accepted it slavikn?

>>How can I make a JLabel automatically write text on multiple lines (if it doesn't fit in a single line)?

You can only do that with font metric calculation, which is what the component at the link i posted does. Using a JTextPane as a label will also do that (through the same mechanism)
0
 
riaancorneliusCommented:
>> But quite apart from that, the accepted answer does *not* answer the given question (no offence i hope riaancornelius ;-)) so i'm wondering why you accepted it slavikn?
None taken. I think it does answer the question, but possibly the question wasn't clear enough. My solution will do conventional word wrapping if the text doesn't fit on a single line.
0
 
CEHJCommented:
>>My solution will do conventional word wrapping if the text doesn't fit on a single line.

Yes, but it's not automatic, which was a requirement stated in the question. What's more, once you've laid it out acceptably by experimentation (which is necessary) it won't necessarily be laid out acceptably on another machine, the layout having been effectively hard-coded
0
 
girionisCommented:
CEHJ I also think it answers the question, since with the <br/> tags you can write text on multiple lines.
0
 
CEHJCommented:
>>since with the <br/> tags you can write text on multiple lines

Yes, but it's *not* automatic. You will only be able to determine the layout through experiment (see my last)
0
 
girionisCommented:
Ok, technically is not a correct comment, but I think the asker wanted something different (as you can also see from his/her last comment) :)
0
 
slaviknAuthor Commented:
Sorry if my question wasn't clear. My problem was as follows: I wrote a component (java bean) that had a lot of stuff on it + a label, and the text on the label could be changed. I needed the text to be written on several lines whenever changed. <html><center>new text goes here</center></html>. did the trick.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 6
  • 4
  • 4
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now