make a JLabel wrap it's text by setting a max width

100,176

Solution 1

No.

You can use HTML in the label, but then you must hard code the break tag.

A better approach is to use a JTextArea and turn wrapping on. You can change the background,foreground, font etc. of the text are to make it look like a label.

Note, this answer is outdated as of at least Java 7.

As per @darren's answer, you simply need to wrap the string with <html> and </html> tags:

myLabel.setText("<html>"+ myString +"</html>");

You do not need to hard-code any break tags. The text wraps as the component resizes.

Solution 2

Yes there are two similar ways (first with css style="width:...px", second with html WIDTH=.......:

1.

labelText = String.format("<html><div style=\"width:%dpx;\">%s</div></html>", width, text);

2.

labelText = String.format("<html><div WIDTH=%d>%s</div></html>", width, text);

Solution 3

or simply use

myLabel.setText("<html>"+ myString +"</html>");

Solution 4

You can use HTML without hard coding break tags if you use paragraph tags instead.

JLabel biglabel = new JLabel("<html><p>A lot of text to be wrapped</p></html>");

Solution 5

JXLabel in the SwingX project supports wrapping

JXLabel label = new JXLabel(somelongtext);
label.setLineWrap(true);  
Share:
100,176

Related videos on Youtube

Aly
Author by

Aly

SOreadytohelp

Updated on July 05, 2022

Comments

  • Aly
    Aly almost 2 years

    I have a JLabel which has a lot of text on it. Is there a way to make the JLabel have a max width so that it will wrap the text to make it not exceed this width?

    Thanks

  • Daniel Rikowski
    Daniel Rikowski about 13 years
    That's not entirely true. If using HTML you can hard code the break tags, but if you use HTML and assign the maximum size, the text will get wrapped automatically.
  • Craigo
    Craigo over 12 years
    If you decide to hard code break tags, make sure you use <br> and not <br />, as Java 5 doesn't like the latter.
  • Peter Dolberg
    Peter Dolberg about 12 years
    The whole thing about using html to make text wrap feels like a hack, but it's the easiest way to make it work. Setting the size in the div is exactly what I needed to make the JLabel wrap with a maximum width without any 3rd party libraries or overly-complicated hacks.
  • Matthieu
    Matthieu almost 11 years
    With JDK 7 that was sufficient, thanks! JLabel lblTitle = new JLabel("<html>My very very very long title text</html>");
  • Andrii Chernenko
    Andrii Chernenko over 10 years
    What if window size changes? Text has to be reflown according to new label width, but with this solution you would have to set it again.
  • shareef
    shareef almost 9 years
    i like the second idea because the first may break a word to half and what i want is word wrapping
  • Rangi Keen
    Rangi Keen over 8 years
    If you do this, you should make sure any HTML entities in myString are first escaped. If myString is something like "Value < 5" it will appear as "Value 5".
  • Bram Vanroy
    Bram Vanroy over 8 years
    For some reason this didn't work for me. p-tags following a heading, and text isn't wrapped. I do have horizontal scrolling disabled so maybe that's got something to do with it.
  • Erick Robertson
    Erick Robertson almost 8 years
    You don't need the p tags.
  • Andrew Thompson
    Andrew Thompson over 7 years
    "You can use HTML in the label, but then you must hard code the break tag." This is wrong. CSS can specify a preferred width, then the string will wrap on word, automatically, into as many lines as needed.
  • SapuSeven
    SapuSeven over 5 years
    This is not text wrapping.
  • Jiří
    Jiří over 5 years
    </html> btw :)