Solved

JLabel - automatic word-wrap

Posted on 2006-07-21
16
5,811 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 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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

628 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