Java Swing How to - Create JLabel from HTML with img tags








Question

We would like to know how to create JLabel from HTML with img tags.

Answer

import java.awt.BorderLayout;
/*from  www .  j av  a 2s .c o m*/
import javax.swing.JFrame;
import javax.swing.JLabel;

public class Main{
  public static void main(String[] args)  {
      JFrame frame = new JFrame();
      frame.setLayout(new BorderLayout());
      JLabel label = new JLabel(
          "<html>"
          + "<img src=\""
          + Main.class.getResource("/resource/path/to/image1.jpg")
          + "\">"
          + "<img src=\""
          + Main.class.getResource("/resource/path/to/image2.jpg")
          + "\">"
          + "The text</html>");
      frame.add(label, BorderLayout.CENTER);
      frame.setBounds(100, 100, 200, 100);
      frame.setVisible(true);
   }
 }