views:

417

answers:

2

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

+3  A: 

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.

camickr
+1  A: 

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);
Alexander.Berg