File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT / JFace and the fly likes JLabel Word Wrap Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Swing / AWT / SWT / JFace
Reply Bookmark "JLabel Word Wrap" Watch "JLabel Word Wrap" New topic
Author

JLabel Word Wrap

Derek Boring
Ranch Hand

Joined: Aug 15, 2006
Posts: 56
I have a JLabel in a JScrollPane. I want to JLabel to vertically, but not horizontally. I've tried setting the JScrollPane to use only a vertical scroll bar, setting the JLabel's maximum size, and even creating my own word wrap method, but no matter what I try, the JLabel keeps going wider then the visible area.

I did some internet searches and found that putting text in HTML tags (<HTML>My Text</HTML> would make it automatically wrap, but the JLabel still behaves the same anyway. Can somebody please help me with this?

BTW, the component needs to display HTML formatted text, so it either has to be a JLabel or another text component that can use HTML formatting.

I greatly appreciate any and all help. Thanks guys.


Life is 10% what happens and 90% how you react to it.<br /> <br />Attitude is everything.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Try using a JEditorPane set is set to be not editable. You might need to set the colors so that it looks like a label.

At work we have an AutoWrappingLabel class that uses a JTextArea internally. I can show you the code on Monday...


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Mhh, ok, I just see that we changed it from using a JTextArea to using a custom made component, because of problems with determining the required size. If you are still interested, I can post the code, but it's quite a lot (~300LOC), and won't handle HTML.
Gregg Bolinger
Sheriff

Joined: Jul 11, 2001
Posts: 15013

Putting JLabel text in HTML doesn't make it automatically do anything. You would still need to tell the HTML to break (<br /> or <p /> or whatever).


My Blog | DZone Articles
Chris Beihl
Greenhorn

Joined: Jan 02, 2007
Posts: 4
I've found that whenever I use html tags within a JLabel, it will wordwrap when resized, but when using a JLabel without html and it is resized to smaller than its preferred size it cuts off the end and add an ellipsis "...".

If you are using html tags and your JLabel is still not word wrapped I suspect that your layout manager is not resizing the JLabel below its preferred size. Some layout managers resize the components and some just clip the edges off.
Derek Boring
Ranch Hand

Joined: Aug 15, 2006
Posts: 56
I've tried all those things (except the JTextArea) and I still can't get it to word wrap. Any more ideas?
Gregg Bolinger
Sheriff

Joined: Jul 11, 2001
Posts: 15013

Well, Derek, there are several things that can cause this not to work. I tried messing around with some sample code and here is what I've figured out.

1. Forgetting the JScrollPane, since java 1.4, surrounding JLabel text with "<html></html>" tags will cause the JLabel's text to wrap if the width of the container is too narrow to display all the text on one line.

2. Putting a JLabel inside a JScrollPane with a JScrollPane.HORIZONTAL_SCROLLBAR_NEVER won't wrap the text. I'm not 100% sure why.

The best solution I have found is here which elludes to what Ilja was saying. But its not real pretty.
Derek Boring
Ranch Hand

Joined: Aug 15, 2006
Posts: 56
Hey, I figured it out guys!!!

After reading a bit more, I came up with a slight suspision that JLabel wasn't being sized properly, so I did an experiment: I made a JPanel and set the JScrollPane to show the contents of the JPanel. Then I set the LayoutManager of the JPanel to null and added the JLabel to it. Then I made a ComponentListener and set it to resize the JLabel to the size of the right pane of the JSplitPane everytime the JSplitPane is resized. The just fill with any text (formatted or not, but must be in <HTML> </HTML> tags) and it will automatically wrap.

Thanks for all your help guys! I appreciate it!
Alex Kruglov
Greenhorn

Joined: May 06, 2008
Posts: 8
Dmitry Zhelezov
Greenhorn

Joined: Dec 29, 2008
Posts: 3
try the following:

[code]JLabel label = new JLabel("<html><table><td >
Dmitry Zhelezov
Greenhorn

Joined: Dec 29, 2008
Posts: 3
try the following:

[code]
JLabel label = new JLabel("<html><table><td width='\" + width + ""\>Some very very very very very long text </td></table></html>")
[/code]
where width is width you want label to fit on. i used parent panel width - borders width.

This message was edited 1 time. Last update was at by Dmitry Zhelezov

Tony Fecteau
Greenhorn

Joined: Aug 27, 2010
Posts: 1
Use html tags and set preferred size to 1x1. It will automatically resize
 
 
subject: JLabel Word Wrap
 
MyEclipse, The Clear Choice