• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JLabel Word Wrap

 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've tried all those things (except the JTextArea) and I still can't get it to word wrap. Any more ideas?
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
try the following:

[code]JLabel label = new JLabel("<html><table><td >
 
Dmitry Zhelezov
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Use html tags and set preferred size to 1x1. It will automatically resize
 
Ranch Hand
Posts: 123
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You don't have any opening <html> tag in there.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
> 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.
 
no wonder he is so sad, he hasn't seen this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic