Loading images from jars for Swing HTML

前端 未结 4 671
余生分开走
余生分开走 2020-11-27 07:07

While this answer works to load images from Jar files for ImageIcons, I cannot seem to get the right path for images referenced in Swing HTML.

This disp

相关标签:
4条回答
  • 2020-11-27 07:23

    Without actually having tried it, I would assume that the HTML renderer can access your image if you include the resource URL in your HTML code:

    String p = getClass().getResource("icons/folder_link.png" ).toString();
    new JLabel("<html><table cellpadding=0><tr><td><img src='" + p + "'></td></tr><tr><td>100</td></tr></table></html>") );
    
    0 讨论(0)
  • 2020-11-27 07:38

    URL is the secret

    Try this mate:

    URL p = getClass().getResource("icons/folder_link.png" );
    new JLabel("<html><table cellpadding=0><tr><td><img src='" + p + "'></td></tr><tr><td>100</td></tr></table></html>") );
    

    Then you could also do this:

    Icon topIcon = new ImageIcon(p);
    

    and then set this icon as the icon for your JLabel if you want to do that!

    0 讨论(0)
  • 2020-11-27 07:40

    Answer expanded and moved to Is it possible/how to embed and access HTML Files in a JAR?

    0 讨论(0)
  • 2020-11-27 07:41

    Because even the original demo from Sun for using HTML in Swing does not embed the images in HTML (for generating buttons with image icons), I doubt that there is even support for displaying images in place. I remember reader "limited subset of HTML" somewhere, but can't find a reference right now.

    Edit: Please see Andrew's comment and answer, it really works.

    0 讨论(0)
提交回复
热议问题