make a JLabel wrap it's text by setting a max width
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);
Related videos on Youtube
Comments
-
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
-
Leif Gruenwoldt almost 14 years
-
-
Daniel Rikowski about 13 yearsThat'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 over 12 yearsIf 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 about 12 yearsThe 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 almost 11 yearsWith JDK 7 that was sufficient, thanks!
JLabel lblTitle = new JLabel("<html>My very very very long title text</html>");
-
Andrii Chernenko over 10 yearsWhat 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 almost 9 yearsi like the second idea because the first may break a word to half and what i want is word wrapping
-
Rangi Keen over 8 yearsIf you do this, you should make sure any HTML entities in
myString
are first escaped. IfmyString
is something like "Value < 5" it will appear as "Value 5". -
Bram Vanroy over 8 yearsFor 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 almost 8 yearsYou don't need the p tags.
-
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 over 5 yearsThis is not text wrapping.
-
Jiří over 5 years
</html>
btw :)