aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes JLabel Word Wrap Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JLabel Word Wrap" Watch "JLabel Word Wrap" New topic
Author

JLabel Word Wrap

Derek Boring
Ranch Hand

Joined: Aug 15, 2006
Posts: 65
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.
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
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15300
    
    6

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).


GenRocket - Experts at Building Test Data
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: 65
I've tried all those things (except the JTextArea) and I still can't get it to word wrap. Any more ideas?
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15300
    
    6

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: 65
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.
Tony Fecteau
Greenhorn

Joined: Aug 27, 2010
Posts: 1
Use html tags and set preferred size to 1x1. It will automatically resize
Aakash Parashar
Ranch Hand

Joined: Jul 25, 2005
Posts: 123

Hi Everybody,
I am using Spring to make a Java Desktop application and using xml for that. I want to add a JLabel, and I want JLabel to wrap the text if its larger.

As suggested above I enriched html tags in the value of text to displayed in xml file but its not working.
Issue:

Text is not getting wrapped ending with ellipses.
Secondly, its displaying <html>

15/5B Samved Building and so on instead of displaying only the required text. Main problem is JLabel is not able to interpret <html> tags and render the painted value.



Please see the xml tag used by me and suggest.



Your's Aakash
SCJP 1.4 96%, SCBCD 1.3 94%, http://java-application-programming.blogspot.in
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19784
    
  20

You don't have any opening <html> tag in there.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Tejas. patel
Greenhorn

Joined: Aug 01, 2012
Posts: 2
HI All,
It is true that to use html tags for word wrap in jlabel when text was static but,
what about the dynamic text?
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> what about the dynamic text?

for normal dynamic text you would use
label.setText(..);
so, you would just wrap in it html tags.

if your question is you don't know where the <br> tag goes,
perhaps using a JTextArea (set up to look like a JLabel), with
line/word wraps set might work.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JLabel Word Wrap