Loading Images from Jars for Swing HTML

Loading images from jars for Swing HTML

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>") );

Loading images from jars for Swing HTML

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>") );

Swing - Include images in JAR file

You are accessing your file through its path on disk before it gets put into a JAR. You need to use a resource loader as explained in:
How to read a file from jar in Java?

JEditorPane with inline image from jar file

I got it finally working.

public class ResourceConnection extends URLConnection {
protected ResourceConnection(URL url) {
super(url);
}

@Override
public void connect() throws IOException {
connected = true;
}

@Override
public InputStream getInputStream() throws IOException {
return ResourceConnection.class.getResource(url.getFile()).openConnection().getInputStream();
}
}

Thanks to Andrew Thompson for the idea to work with streams.

how to load the images in java runnable jar?

Use getResourceAsStream

http://download.oracle.com/javase/tutorial/uiswing/components/icon.html

http://www.jugpadova.it/articles/2006/02/05/accessing-a-resource-within-a-jar

Loading resources like images while running project distributed as JAR archive

First of all, change this line :

image = ImageIO.read(getClass().getClassLoader().getResource("resources/icon.gif"));

to this :

image = ImageIO.read(getClass().getResource("/resources/icon.gif"));

More info, on as to where lies the difference between the two approaches, can be found on this thread - Different ways of loading a Resource

For Eclipse:

  • How to add Images to your Resource Folder in the Project

For NetBeans:

  • Handling Images in a Java GUI Application
  • How to add Images to the Project

For IntelliJ IDEA:

  • Right-Click the src Folder of the Project. Select New -> Package
  • Under New Package Dialog, type name of the package, say resources. Click OK
  • Right Click resources package. Select New -> Package
  • Under New Package Dialog, type name of the package, say images. Click OK
  • Now select the image that you want to add to the project, copy it. Right click resources.images package, inside the IDE, and select Paste
  • Use the last link to check how to access this file now in Java code. Though for this example, one would be using

    getClass().getResource("/resources/images/myImage.imageExtension");

  • Press Shift + F10, to make and run the project. The resources and images folders, will be created automatically inside the out folder.

If you are doing it manually :

  • How to add Images to your Project
  • How to Use Icons
  • A Little extra clarification, as given in this answer's first
    code example.

QUICK REFERENCE CODE EXAMPLE(though for more detail consider, A little extra clarification link):

package swingtest;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.*;

/**
* Created with IntelliJ IDEA.
* User: Gagandeep Bali
* Date: 7/1/14
* Time: 9:44 AM
* To change this template use File | Settings | File Templates.
*/
public class ImageExample {

private MyPanel contentPane;

private void displayGUI() {
JFrame frame = new JFrame("Image Example");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

contentPane = new MyPanel();

frame.setContentPane(contentPane);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}

private class MyPanel extends JPanel {

private BufferedImage image;

public MyPanel() {
try {
image = ImageIO.read(MyPanel.class.getResource("/resources/images/planetbackground.jpg"));
} catch (IOException ioe) {
ioe.printStackTrace();
}
}

@Override
public Dimension getPreferredSize() {
return image == null ? new Dimension(400, 300): new Dimension(image.getWidth(), image.getHeight());
}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(image, 0, 0, this);
}
}

public static void main(String[] args) {
Runnable runnable = new Runnable() {
@Override
public void run() {
new ImageExample().displayGUI();
}
};
EventQueue.invokeLater(runnable);
}
}

showing images on jeditorpane (java swing)

You have to provide type, and get the resource. That's all. My tested example, but I'm not sure about formating. Hope it helps:

import java.io.IOException;
import javax.swing.JEditorPane;
import javax.swing.JFrame;

public class Test extends JFrame {

public static void main(String[] args) throws Exception {
Test.createAndShowGUI();
}

private static void createAndShowGUI() throws IOException {

JFrame.setDefaultLookAndFeelDecorated(true);

JFrame frame = new JFrame("HelloWorldSwing");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

String imgsrc =
Test.class.getClassLoader().getSystemResource("a.jpg").toString();
frame.getContentPane().add(new JEditorPane("text/html",
"<html><img src='"+imgsrc+"' width=200height=200></img>"));
frame.pack();

frame.setVisible(true);
}
}

use html file from jar in java

File won't take a URL as reference. Embedded resources like this aren't actually files, they are InputStreams to the resource.

Depending on how you're showing them will determine what you need to do.

For example, JEditorPane takes a URL via its setPage method

UPDATED

You should be able to load the pages directly into the editor pane using something like...

// editor pane is a reference to a JEditorPane
editorPane.setPage(getClass.getResource("/path is the src folder/"));

attach html files ,images and index files in executable jar

You can use e.g.

ImageIcon image = (new ImageIcon(getClass().getResource("yourpackage/mypackage/image.gif")));

In general, you can retrieve an InputStream in the following way:

InputStream is = this.getClass().getResourceAsStream("yourpackage/mypackage/file.html");

Then use the stream or URL to work with the files.



Related Topics



Leave a reply



Submit