aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes How To Make JLabel automatically wrap to next line Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "How To Make JLabel automatically wrap to next line" Watch "How To Make JLabel automatically wrap to next line" New topic
Author

How To Make JLabel automatically wrap to next line

Frank Li
Greenhorn

Joined: Aug 07, 2003
Posts: 10
I have a JLabel text displayed in an Introductory text in a dialog. How can I make it fit in with the dialog width and automatically wrap to next line
nicely?
Right now, when the text is long, it changes my dialog size (width) to fit the text in. That make the dialog looks bad.
Is ther any better way to do this ? Maybe I should use JTextArea not JLabel?
Please help. Thanks!
VIJAY Yadlapati
Ranch Hand

Joined: Aug 04, 2003
Posts: 175
You can use HTML text as Label text to your label
VIJAY Yadlapati
Ranch Hand

Joined: Aug 04, 2003
Posts: 175
You can use HTML as Label text.
VIJAY Yadlapati
Ranch Hand

Joined: Aug 04, 2003
Posts: 175
You can use HTML for Label text. Here is the code sample
label.setText(<HTML> Line1<br> Line2</HTML>");
Frank Li
Greenhorn

Joined: Aug 07, 2003
Posts: 10
Hi YIJAY,
Thanks for your reply. The HTML solution does not really help because I don't know where to break. My label could be very long and it is translatable. In different language, the length varies.
I have figured out that if I use JTextArea and
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
works for me.
Maybe there are better way to do so.
Thanks a lot!
Matthew Taylor
Rancher

Joined: Jun 13, 2004
Posts: 110
You can use HTML <p> tags to make the text break wherever it needs to. For example...

This should do what you want.
[ October 20, 2004: Message edited by: Matthew Taylor ]

Grails Consultant
http://dangertree.net
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
LineBreakMeasurer
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: How To Make JLabel automatically wrap to next line