Solved

JLabel - automatic word-wrap

Posted on 2006-07-21
16
5,787 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
 
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

744 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now