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.
Attitude is everything.
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
Joined: Jul 11, 2001
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.
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.
Joined: Aug 15, 2006
I've tried all those things (except the JTextArea) and I still can't get it to word wrap. Any more ideas?
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.
Joined: Aug 15, 2006
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.
[code]JLabel label = new JLabel("<html><table><td >
Joined: Dec 29, 2008
try the following:
JLabel label = new JLabel("<html><table><td width='\" + width + ""\>Some very very very very very long text </td></table></html>")
where width is width you want label to fit on. i used parent panel width - borders width.