123

How can I display a newline in JLabel?

For example, if I wanted:

Hello World!
blahblahblah

This is what I have right now:

JLabel l = new JLabel("Hello World!\nblahblahblah", SwingConstants.CENTER);

This is what is displayed:

Hello World!blahblahblah

Forgive me if this is a dumb question, I'm just learning some Swing basics...

Lukas Rotter
  • 4,158
  • 1
  • 15
  • 35
mportiz08
  • 10,206
  • 12
  • 40
  • 42

6 Answers6

198

Surround the string with <html></html> and break the lines with <br/>.

JLabel l = new JLabel("<html>Hello World!<br/>blahblahblah</html>", SwingConstants.CENTER);
Nathan
  • 8,093
  • 8
  • 50
  • 76
freitass
  • 6,542
  • 5
  • 40
  • 44
13

You can try and do this:

myLabel.setText("<html>" + myString.replaceAll("<","&lt;").replaceAll(">", "&gt;").replaceAll("\n", "<br/>") + "</html>")

The advantages of doing this are:

  • It replaces all newlines with <br/>, without fail.
  • It automatically replaces eventual < and > with &lt; and &gt; respectively, preventing some render havoc.

What it does is:

  • "<html>" + adds an opening html tag at the beginning
  • .replaceAll("<", "&lt;").replaceAll(">", "&gt;") escapes < and > for convenience
  • .replaceAll("\n", "<br/>") replaces all newlines by br (HTML line break) tags for what you wanted
  • ... and + "</html>" closes our html tag at the end.

P.S.: I'm very sorry to wake up such an old post, but whatever, you have a reliable snippet for your Java!

TheSola10
  • 697
  • 6
  • 16
3

You can use the MultilineLabel component in the Jide Open Source Components.

http://www.jidesoft.com/products/oss.htm

Aakash
  • 329
  • 1
  • 4
2

JLabel is actually capable of displaying some rudimentary HTML, which is why it is not responding to your use of the newline character (unlike, say, System.out).

If you put in the corresponding HTML and used <BR>, you would get your newlines.

Uri
  • 88,451
  • 51
  • 221
  • 321
  • *sigh* I tried to add backticks to escape your html, however it says I can't edit unless I'm adding at least 6 characters. – AnnanFay Apr 16 '11 at 00:13
  • 1
    @Annan That isn't necessary in HTML. What you're talking about is for supporting XHTML. http://stackoverflow.com/questions/1946426/html-5-is-it-br-br-or-br – arkon May 11 '13 at 17:03
  • @b1naryatr0phy na, the problem was fixed :) The original post had a literal `
    ` tag which was formatted by stack-overflow as a literal line break in the post.
    – AnnanFay May 13 '13 at 04:50
2

You can do

JLabel l = new JLabel("<html><p>Hello World! blah blah blah</p></html>", SwingConstants.CENTER);

and it will automatically wrap it where appropriate.

nevster
  • 6,271
  • 7
  • 35
  • 42
2

Thanks Aakash for recommending JIDE MultilineLabel. JIDE's StyledLabel is also enhanced recently to support multiple line. I would recommend it over the MultilineLabel as it has many other great features. You can check out an article on StyledLabel below. It is still free and open source.

http://www.jidesoft.com/articles/StyledLabel.pdf

jidesoft
  • 202
  • 1
  • 3