Link to home
Start Free TrialLog in
Avatar of slavikn
slavikn

asked on

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
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER CERTIFIED SOLUTION
Avatar of riaancornelius
riaancornelius

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You have to use HTML

new JLabel("<html><body>hey<br>and another hey<br>hey hey.");
typo

new JLabel("<html><body>hey<br>and another hey<br>hey hey.</body></hmtl>");
html is *not* automatic ;-)
Avatar of riaancornelius
riaancornelius

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.
Avatar of slavikn

ASKER

Thanks for the quick replies. I use (need the text centered):
<html><center>Baltic Avenue</center></html>.
thanks slavikn.
>>Thanks for the quick replies. I use (need the text centered):

Where does the word wrap come in then?
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)
>> 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.
>>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
CEHJ I also think it answers the question, since with the <br/> tags you can write text on multiple lines.
>>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)
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) :)
Avatar of slavikn

ASKER

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.