JEditorPane, html, and image

F

Fencer

Hello, I have an application that has an JEditorPane I use for
displaying html. The actual html is stored in a file foo.html together
with an image bar.jpg. These two files are in a directory resources that
is in my CLASSPATH.
This is an eclipse project and I need to be able to export it as a
self-contained, runnable .jar-file. The problem is that the image isn't
displayed. Here's a small test program I've written:

package gui;

import java.io.IOException;

import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.text.html.HTMLDocument;
import javax.swing.text.html.HTMLEditorKit;

public class ATest {
public static void main(String[] args) {
new ATest();
}

private ATest() {
final JFrame frame = new JFrame("atest");

frame.getContentPane().add(createHTMLPane());

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(800, 600);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}

private JScrollPane createHTMLPane() {
HTMLEditorKit htmlEditorKit = new HTMLEditorKit();
HTMLDocument htmlDocument =
(HTMLDocument)htmlEditorKit.createDefaultDocument();
// How should the URL I'm passing to setBase() be constructed?
htmlDocument.setBase(ATest.class.getResource(("/resources")));

JEditorPane editor = new JEditorPane();
editor.setEditable(false);

editor.setEditorKit(htmlEditorKit);
try {
String resPath = "/resources/foo.html";
editor.read(ATest.class.getResourceAsStream(resPath),
htmlDocument);
}
catch (IOException e) {
System.err.println("IOException caught.");
}

return new JScrollPane(editor);
}
}

I think the problem is that I don't know how to use setBase() on the
HTMLDocument properly. How should I construct the URL? The two files are
in a directory "resources" directly under "src" as seen from within
eclipse. No matter if I run the app from eclipse or as an exported
jarfile I still dont see the image (the jarfile does however contain the
directory resources with the html file and the image).

Please help me! This is the only thing stopping me from releasing this
software!

- Fencer
 
A

Albert

Fencer a écrit :
Hello, I have an application that has an JEditorPane I use for
displaying html. The actual html is stored in a file foo.html together
with an image bar.jpg. These two files are in a directory resources that
is in my CLASSPATH.
This is an eclipse project and I need to be able to export it as a
self-contained, runnable .jar-file. The problem is that the image isn't
displayed. Here's a small test program I've written:

package gui;

import java.io.IOException;

import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.text.html.HTMLDocument;
import javax.swing.text.html.HTMLEditorKit;

public class ATest {
public static void main(String[] args) {
new ATest();
}

private ATest() {
final JFrame frame = new JFrame("atest");

frame.getContentPane().add(createHTMLPane());

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(800, 600);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}

private JScrollPane createHTMLPane() {
HTMLEditorKit htmlEditorKit = new HTMLEditorKit();
HTMLDocument htmlDocument =
(HTMLDocument)htmlEditorKit.createDefaultDocument();
// How should the URL I'm passing to setBase() be constructed?
htmlDocument.setBase(ATest.class.getResource(("/resources")));

JEditorPane editor = new JEditorPane();
editor.setEditable(false);

editor.setEditorKit(htmlEditorKit);
try {
String resPath = "/resources/foo.html";
editor.read(ATest.class.getResourceAsStream(resPath),
htmlDocument);
}
catch (IOException e) {
System.err.println("IOException caught.");
}

return new JScrollPane(editor);
}
}

I think the problem is that I don't know how to use setBase() on the
HTMLDocument properly. How should I construct the URL? The two files are
in a directory "resources" directly under "src" as seen from within
eclipse. No matter if I run the app from eclipse or as an exported
jarfile I still dont see the image (the jarfile does however contain the
directory resources with the html file and the image).

Please help me! This is the only thing stopping me from releasing this
software!

Your html file is in "/ressources/" (in the jar), so if the html refer
to the image as "image/bar.jpg" (in <img> tag), the jpg file must be in
"/ressources/image/bar.jpg" (in the jar) if you want it to work.
 
F

Fencer

Your html file is in "/ressources/" (in the jar), so if the html refer
to the image as "image/bar.jpg" (in <img> tag), the jpg file must be in
"/ressources/image/bar.jpg" (in the jar) if you want it to work.

Thanks for your reply. Both the bar.jpg and foo.html are in resources.
But in order to make it easier for someone to try my code I've rewritten
my test program so that the html is a string instead, here it is:

package gui;

import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.text.html.HTMLDocument;
import javax.swing.text.html.HTMLEditorKit;

public class ATest {
public static void main(String[] args) {
new ATest();
}

private ATest() {
final JFrame frame = new JFrame("atest");

frame.getContentPane().add(createHTMLPane());

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(800, 600);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}

private JScrollPane createHTMLPane() {
HTMLEditorKit htmlEditorKit = new HTMLEditorKit();
HTMLDocument htmlDocument =
(HTMLDocument)htmlEditorKit.createDefaultDocument();
try {
htmlDocument.setBase(ATest.class.getResource("/resources"));
}
catch (Throwable t) {
System.err.println("Exception " + t + ": " + t.getMessage());
}

JEditorPane editor = new JEditorPane();
editor.setEditable(false);

editor.setEditorKit(htmlEditorKit);
editor.setText(htmlString);

return new JScrollPane(editor);
}

private String htmlString = "<!DOCTYPE html SYSTEM
\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n"+
"<html xmlns=\"http://www.w3.org/1999/xhtml\">\n"+
" <head><title></title></head>\n"+
" <body>\n"+
" <center>\n"+
" <p>\n"+
" <img src=\"bar.jpg\" alt=\"aaa\"/>\n"+
" </p>\n"+
" </center>\n"+
" <center>\n"+
" <p>\n"+
" <img src=\"/resources/bar.jpg\" alt=\"bbb\"/>\n"+
" </p>\n"+
" </center>\n"+
" </body>\n"+
"</html>";
}

I've validated the html using the W3C validator and it passes. As you
can see I am trying two different paths to the same image in the html,
but neither works (I don't see any images). Tried both from within
Eclipse and running the exported jarfile.
Where am I going wrong?

- Fencer
 
F

Fencer

Your html file is in "/ressources/" (in the jar), so if the html refer
to the image as "image/bar.jpg" (in <img> tag), the jpg file must be in
"/ressources/image/bar.jpg" (in the jar) if you want it to work.

Thanks for your reply. Both the bar.jpg and foo.html are in resources.
But in order to make it easier for someone to try my code I've rewritten
my test program so that the html is a string instead, here it is:

package gui;

import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.text.html.HTMLDocument;
import javax.swing.text.html.HTMLEditorKit;

public class ATest {
public static void main(String[] args) {
new ATest();
}

private ATest() {
final JFrame frame = new JFrame("atest");

frame.getContentPane().add(createHTMLPane());

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(800, 600);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}

private JScrollPane createHTMLPane() {
HTMLEditorKit htmlEditorKit = new HTMLEditorKit();
HTMLDocument htmlDocument =
(HTMLDocument)htmlEditorKit.createDefaultDocument();
try {
htmlDocument.setBase(ATest.class.getResource("/resources"));
}
catch (Throwable t) {
System.err.println("Exception " + t + ": " + t.getMessage());
}

JEditorPane editor = new JEditorPane();
editor.setEditable(false);

editor.setEditorKit(htmlEditorKit);
editor.setText(htmlString);

return new JScrollPane(editor);
}

private String htmlString = "<!DOCTYPE html SYSTEM
\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n"+
"<html xmlns=\"http://www.w3.org/1999/xhtml\">\n"+
" <head><title></title></head>\n"+
" <body>\n"+
" <center>\n"+
" <p>\n"+
" <img src=\"bar.jpg\" alt=\"aaa\"/>\n"+
" </p>\n"+
" </center>\n"+
" <center>\n"+
" <p>\n"+
" <img src=\"/resources/bar.jpg\" alt=\"bbb\"/>\n"+
" </p>\n"+
" </center>\n"+
" </body>\n"+
"</html>";
}

I've validated the html using the W3C validator and it passes. As you
can see I am trying two different paths to the same image in the html,
but neither works (I don't see any images). Tried both from within
Eclipse and running the exported jarfile.
Where am I going wrong?

- Fencer

I seem to have solved this problem by modifying the img src in the html
"on the fly". It works on my development machine both from within the
IDE and as a jarfile. Also tried the jarfile on two different computers
and it works!

- Fencer
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,062
Latest member
OrderKetozenseACV

Latest Threads

Top