Solved

JLabel - automatic word-wrap

Posted on 2006-07-21
16
5,795 Views
Last Modified: 2012-06-27
Hi.

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

Thanks
0
Comment
Question by:slavikn
  • 6
  • 4
  • 4
  • +1
16 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 17153237
0
 
LVL 9

Accepted Solution

by:
riaancornelius earned 50 total points
ID: 17153238
surround your text with <html> and </html> tags.
like <html>yourString</html>
0
 
LVL 35

Expert Comment

by:girionis
ID: 17153240
You have to use HTML

new JLabel("<html><body>hey<br>and another hey<br>hey hey.");
0
ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

 
LVL 35

Expert Comment

by:girionis
ID: 17153244
typo

new JLabel("<html><body>hey<br>and another hey<br>hey hey.</body></hmtl>");
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17153261
html is *not* automatic ;-)
0
 
LVL 9

Expert Comment

by:riaancornelius
ID: 17153300
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
 
LVL 1

Author Comment

by:slavikn
ID: 17153334
Thanks for the quick replies. I use (need the text centered):
<html><center>Baltic Avenue</center></html>.
0
 
LVL 9

Expert Comment

by:riaancornelius
ID: 17153343
thanks slavikn.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17153689
>>Thanks for the quick replies. I use (need the text centered):

Where does the word wrap come in then?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17153731
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
 
LVL 9

Expert Comment

by:riaancornelius
ID: 17153769
>> 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
 
LVL 86

Expert Comment

by:CEHJ
ID: 17153865
>>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
 
LVL 35

Expert Comment

by:girionis
ID: 17153885
CEHJ I also think it answers the question, since with the <br/> tags you can write text on multiple lines.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17153947
>>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
 
LVL 35

Expert Comment

by:girionis
ID: 17153967
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
 
LVL 1

Author Comment

by:slavikn
ID: 17155317
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

Featured Post

ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Python Assistance 7 80
Windows 10 IE Certificate Issue 10 45
Java string replace 11 46
What's wrong with this code? 4 23
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question