views:

84

answers:

4

Hi, I am trying to display a JLabel which has a few lines of text and an image as follows:

String html = "<html> hello </br> <img src = \"/absolute/path/here\" height = \"30\"  width =\"40\"/> </html>";
JLabel l = new JLabel(html);

For the image all I get is a broken image, is it possible to nest img tags inside a JLabel?

EDIT: I want to add multiple images to the JLabel so I don't think the use of an ImageIcon will do here.

Thanks

+1  A: 

Embedded images are not supported in the HTML. Thus, you have to use setIcon or supply an ImageIcon to the JLabel constructor; the HTML cannot have an IMG tag.

  JLabel imageLabel =
  new JLabel(labelText,
             new ImageIcon("path/to/image.gif"),
             JLabel.CENTER);

In your case you need to use JTextPane to display HTML. See tutorial here

uthark
What if I want to have multiple images, on different lines on the label?
Aly
+1  A: 

Use a JEditorPane to display the HTML. You can change the background, forground, font etc so it looks like a label.

camickr
+1  A: 

Unless you're happy with JEditorPane, you're basically looking at a full webbrowser inside of Swing.

Ideally, you would use JWebPane which would be a WebKit view as a Swing component, but it isn't out yet. The most recent information I could find was this blog post.

The DJ project allows embedding the platform's native browser in Swing. It uses Internet Explorer on Windows and XULRunner on Linux. It does not have any support for Mac.

Frederik
+1  A: 

Rather then try to have multiple images on a single JLabel (which you cant) why not just have many JLabels, each with one image (as uthark described) and then group all the labels together on a single JPanel. That should give you the effect you are looking for with only minimal additional complexity.

Kris