4
JLabel label = new JLabel("<html><body>Hello world</body></html>");

shows nothing.

If I get rid of the tags, it shows plain text (as expected), so the JLabel is definitely being added and shown on the window.

Same for:

JEditorPane jep = new JEditorPane("text/html", "<html><body>Hello world</body></html>");

Any ideas?

I'm using java-6-openjdk with Eclipse. More details:

matt@matt-laptop:~$ java -version
java version "1.6.0_20"
OpenJDK Runtime Environment (IcedTea6 1.9.5) (6b20-1.9.5-0ubuntu1~10.04.1)
OpenJDK Server VM (build 19.0-b09, mixed mode)

matt@matt-laptop:~$ javac -version
javac 1.6.0_24
Matt
  • 11,157
  • 26
  • 81
  • 110
  • 2
    Your code works for me on Java(TM) SE Runtime Environment. – dogbane Feb 25 '11 at 15:43
  • _If you want to mix fonts or colors within the text, or if you want formatting such as multiple lines, you can use HTML. HTML formatting can be used in all Swing buttons, menu items, labels, tool tips, and tabbed panes, as well as in components such as trees and tables that use labels to render text._ **What formatting were you trying to achieve?** – Alpine Feb 25 '11 at 16:00
  • 1
    Refer this example [HtmlDemo.java](http://download.oracle.com/javase/tutorial/uiswing/examples/components/HtmlDemoProject/src/components/HtmlDemo.java) – Alpine Feb 25 '11 at 16:05
  • @Alpine, that example actually worked for me! Now I just need to find out what it's doing differently.. – Matt Feb 25 '11 at 16:24
  • This bug report may be relevant (http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6993691). – Andrew Thompson Feb 25 '11 at 17:03

2 Answers2

6

If you want to mix fonts or colors within the text, or if you want formatting such as multiple lines, you can use HTML. HTML formatting can be used in all Swing buttons, menu items, labels, tool tips, and tabbed panes, as well as in components such as trees and tables that use labels to render text.

Refer this example HtmlDemo.java


In your example

 JLabel label = new JLabel("<html><body>Hello world</body></html>"); 

you are not applying any formating like <b>bold</b> or <i>italic</i> etc..

also <body></body> is not required.

Hope it helps.

Alpine
  • 3,838
  • 1
  • 25
  • 18
1

I have tested your JLabel and JEditorPane code in Eclipse using the latest Windows JDK, using several different look-and-feels, but they all work just fine. I know you're using OpenJDK but I can't get hold of a version for Windows. I'm guessing there is a difference in the two implementations.

Might I suggest wrapping your text inside a paragraph, such as "<html><body><p>Hello world</p></body></html>"?

BoffinBrain
  • 6,337
  • 6
  • 33
  • 59