57

Possible Duplicate:
Multiline text in JLabel

I want to do this:

JLabel myLabel = new JLabel();
myLabel.setText("This is\na multi-line string");

Currently this results in a label that displays

This isa multi-line string

I want it to do this instead:

This is
a multi-line string

Any suggestions?

Thank you


EDIT: Implemented solution

In body of method:

myLabel.setText(convertToMultiline("This is\na multi-line string"));

Helper method:

public static String convertToMultiline(String orig)
{
    return "<html>" + orig.replaceAll("\n", "<br>");
}
Community
  • 1
  • 1
bguiz
  • 27,371
  • 47
  • 154
  • 243

7 Answers7

73

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.

Peter Lang
  • 54,264
  • 27
  • 148
  • 161
21
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

Whimusical
  • 6,401
  • 11
  • 62
  • 105
  • 1
    It's not ugly at all! I ended up building on this by having my component extend JScrollPane which in turn wrapped a JTextArea configured exactly as you have here. Wrapping in the JScrollPane allows the thing to shrink back down after it has be resized to a larger width. – matt forsythe Apr 21 '15 at 21:08
  • 2
    Thanks for the useful answer! To beautify it a bit, you could also remove the line border, add insets, and align center verically: setBorder(new EmptyBorder(5, 5, 5, 5)); setAlignmentY(JLabel.CENTER_ALIGNMENT); – Mariana Sep 19 '16 at 08:06
  • I had to call `super(text + System.lineSeparator());` to prevent the bottom of the label from getting cut-off. – Mr. Polywhirl Jul 15 '22 at 14:01
7

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>"); 
Austyn Mahoney
  • 11,398
  • 8
  • 64
  • 85
5

JLabel can accept html code. Maybe you can try to use the <br> tag.

Example:

JLabel myLabel = new JLabel();
myLabel.setText("<html> This is a <br> multi-line string </html>");
alexandre-rousseau
  • 2,321
  • 26
  • 33
5

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

JLabel label = new JLabel("<html>First Line<br>Second Line</html>"); 
Marco Schmid
  • 174
  • 2
5

The problem with using html in JLabel or any Swing component is that you then have to style it as html, not with the usual setFont, setForeground, etc. If you're ok with that, fine.

Otherwise you can use something like MultilineLabel from JIDE, which extends JTextArea. It's part of their open source Commom Layer.

Geoffrey Zheng
  • 6,562
  • 2
  • 38
  • 47