Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

JLabel - automatic word-wrap

Posted on 2006-07-21
16
Medium Priority
?
5,819 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
  • 4
  • +1
16 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 17153237
0
 
LVL 9

Accepted Solution

by:
riaancornelius earned 200 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
The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

 
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

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

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

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

715 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