Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 534
Aly Avatar asked Mar 10 '10 21:03

Aly


People also ask

How do you make a JLabel text wrap?

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.

How do you change the width of a JLabel?

You can set a fixed the size by setting the minimum, preferred and maximum size: setMinimumSize(width, height); setPreferredSize(width, height); setMaximumSize(width, height);

How do you make a text wrap in Jtextfield?

To wrap the lines of JTextArea we need to call the setLineWrap(boolean wrap) method and pass a true boolean value as the parameter. The setWrapStyleWord(boolean word) method wrap the lines at word boundaries when we set it to true .

How do I get JLabel to text next line?

Surround the string with <html></html> and break the lines with <br/> . just a little correction: use <br /> instead of just <br> ... this is recommended way of doing it (to not miss any closing tags)...


1 Answers

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.

like image 92
camickr Avatar answered Sep 20 '22 19:09

camickr