Question: I am displaying some text in a JLabel. Basically I am generating that text dynamically, and then I apply some HTML tags (e.g., `BR` and `B`) to format the text. Finally I assign this formatted text to my JLabel.

Now I want my Jlabel to automatically wrap the text to the next line when it reaches the end of screen, like the "Word Wrap" feature in Note Pad.

How can I do that?

Answer: 
A width can be set for the `body` using HTML styles (CSS). This in turn will determine the number of lines to render and, from that, the preferred height of the label.

Setting the width in CSS avoids the need to compute where line breaks should occur in (or the best size of) the label.

```
import javax.swing.*;

public class FixedWidthLabel {

    public static void main(String[] srgs) {
        final String s = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean eu nulla urna. Donec sit amet risus nisl, a porta enim. Quisque luctus, ligula eu scelerisque gravida, tellus quam vestibulum urna, ut aliquet sapien purus sed erat. Pellentesque consequat vehicula magna, eu aliquam magna interdum porttitor. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Sed sollicitudin sapien non leo tempus lobortis. Morbi semper auctor ipsum, a semper quam elementum a. Aliquam eget sem metus.";
        final String html = "<html><body style='width: %1spx'>%1s";

        Runnable r = () -> {
            JOptionPane.showMessageDialog(
                    null, String.format(html, 200, s));
            JOptionPane.showMessageDialog(
                    null, String.format(html, 300, s));
        };
        SwingUtilities.invokeLater(r);
    }
}

```

![enter image description here](https://i.stack.imgur.com/9tzxu.png) ![enter image description here](https://i.stack.imgur.com/9mfvE.png)
I like the JTextArea approach mentioned above, because it resizes nicely at SOUTH in a BorderLayout panel (as long as the CENTER component can take up the slack if the number of lines in the JTextArea changes).

However, in Nimbus L&F there is a catch in setting the JTextArea's background colour. It seems that Nimbus uses magic colours (extended class of java.awt.Color) that go transparent in JTextArea. So if you are copying the background colour from a JPanel to the JText area you need to convert the Color to ARGB and back to Color. The following code works for me in all the L&Fs in my JRE (Nimbus, CDE Motif, Metal, Mac OS X):

```
    JTextArea ta = new JTextArea(text);
    ta.setEditable(false);
    ta.setLineWrap(true);
    ta.setWrapStyleWord(true);
    JLabel lb = new JLabel();
    Font f = lb.getFont();
    ta.setFont(f.deriveFont(f.getSize2D() * 0.9f));
    ta.setBorder(lb.getBorder());
    ta.setBackground(new Color(lb.getBackground().getRGB(), true));
    ta.setForeground(new Color(lb.getForeground().getRGB(), true));
    ta.setOpaque(lb.isOpaque());

```

I made the font a bit smaller. Of course you can keep JLabel's font size if you want.