hello <...">

Maybe JLabel have img tags

I am trying to display a JLabel that has several lines of text and 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 whole image I get is a broken image, is it possible to embed img tags inside JLabel?

EDIT: I want to add some images to JLabel, so I don't think that ImageIcon will be used here.

thank

+3
source share
6 answers

JLabel, JLabels, ( uthark), JPanel. , .

+1
File f = new File("C:\image.jpg"); 
jLabel1.setText("<html><img src=\"file:"+file.toString()+"\">");

. , , . .

+4

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

, JLabel. , . file:, java class.getResource("/your/path"). , .

import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JLabel; 

public class MultipleImagesExample
{

  public static void main(String[] args)
  {

      JFrame frame = new JFrame();
      frame.setLayout(new BorderLayout());
      JLabel label = new JLabel(
          "<html>"
          + "<img src=\""
          + MultipleImagesExample.class.getResource("/resource/path/to/image1")
          + "\">"
          + "<img src=\""
          + MultipleImagesExample.class.getResource("/resource/path/to/image2")
          + "\">"
          + "The text</html>");
      frame.add(label, BorderLayout.CENTER);
      frame.setBounds(100, 100, 200, 100);
      frame.setVisible(true);
   }

 }

HTML java, xhtmlrenderer.

+3

JEditorPane, - Swing.

JWebPane, WebKit Swing, . , , - .

DJ Swing. Internet Explorer Windows XULRunner Linux. Mac.

+2

JEditorPane HTML. , , .., .

+1

Inline images are not supported in HTML. Thus, you need to use setIcon or provide ImageIcon for the JLabel constructor; 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 the tutorial here.

0
source

Source: https://habr.com/ru/post/1737840/


All Articles