Adding images in JLabel, Using HTML tags.

I was trying to add multiple images in a JLabel, which was only possible by adding HTML code in label. Most of the HTML tag works very similar as web page, but image URL is quite different when we have to provide relative address for image tag. Example goes here:

import javax.swing.JFrame;
import javax.swing.JLabel;
/**
 *
 * @author Prashant
 */
public class MultiImage extends JFrame{
 JLabel lbl;
 public MultiImage()
 {
 this.setLayout(null);

 String str="<html><img src='file:images/emoticons/01.png'>" +
 "<font color='red' size='4'><b>Happy Life</b></font>" +
 "<img src='file:images/emoticons/01.png'></html>";
 lbl=new JLabel(str);
 lbl.setBounds(15, 10, 120, 25);
 this.add(lbl);
 setSize(150,150);
 setVisible(true);
 setResizable(false);
 this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
 }
 public static void main(String[] args) {
 new MultiImage();
 }
}

Output:


Advertisements

3 Responses to Adding images in JLabel, Using HTML tags.

  1. nisha says:

    the pgm is nt running in my system. there is prblm wid the image linkin only can u help me for dat.

    • Prashant says:

      For the given example, i have images/emoticons/01.png url in the same location where the class file exist. So there is images directory and emoticons inside images and 01.png inside emoticons directory.
      I think you might had mistaken to for URL. if not, plz send me just that label code.

  2. F3nIx0x3 says:

    Hi, I am trying create and html with java. I already have the actionlistener so that the image can be created on my container but now can you help me how to save the image show it on an html?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: