Java swing: Multiline labels?

The direct procedure of writing a multi-line text in a jlabel is:

JLabel label = new JLabel("<html>First Line<br>Second Line</html>"); 

public class JMultilineLabel extends JTextArea{
    private static final long serialVersionUID = 1L;
    public JMultilineLabel(String text){
        super(text); // According to Mr. Polywhirl, this might improve it -> text + System.lineSeparator()
        setEditable(false);  
        setCursor(null);  
        setOpaque(false);  
        setFocusable(false);  
        setFont(UIManager.getFont("Label.font"));      
        setWrapStyleWord(true);  
        setLineWrap(true);
        //According to Mariana this might improve it
        setBorder(new EmptyBorder(5, 5, 5, 5));  
        setAlignmentY(JLabel.CENTER_ALIGNMENT);
    }
} 

It totally looks the same for me, but its ugly


Another easy way (but changes the text style a bit) is to use a <pre></pre> html block.

This will persist any formatting the user entered if the string you are using came from a user input box.

Example:

JLabel label = new JLabel("<html><pre>First Line\nSecond Line</pre></html>"); 

You can use HTML in JLabels. To use it, your text has to start with <html>.

Set your text to "<html>This is<br>a multi-line string" and it should work.

See Swing Tutorial: JLabel and Multiline label (HTML) for more information.