Solved

JLabel - automatic word-wrap

Posted on 2006-07-21
16
5,797 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
jar file executable 12 58
object oriented programming comparison 5 77
null output 3 35
Java 8 to Java 6 8 20
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…
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…
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…
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 …

808 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