I have a user (in a large university environment) that is using an
application which uses JEditorPane to display reports in HTML format.
Initiatially, the applicaton was set up with :
URL url = new URL("file:test.htm");
but this caused a URL exception, the exact likes of which I don't
exactly recall. To get around this problem, I changed the statement to :
URL("file","localhost","test.htm");
and the reports were displayed with no error messages. I had chaulked it
up to some unknown network thing in his environment. Now, however, I
would like to add reports which contain jpgs and I am finding the
URL("file","localhost","test.htm");
form does not render the jpgs, it gives the image not found symbol.
However, if you use:
URL("file","localhost","test.htm");
the jpgs are rendered fine.
Can anyone 1) explain what is going on and 2) suggest a work around.
Code for a test class follows, along with a simple HTML file. You can
supply your own jpg (in place of test.jpg). Any help would be greatly
appreciated. Thanks - Lou
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;
import javax.swing.text.html.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
public class HTML extends JFrame implements HyperlinkListener
{
public HTML() {
super("HTML Widget");
setVisible(false);
JPanel p = new JPanel(new BorderLayout());
try {
URL url = new URL("file:test.htm");
//URL url = new URL("file","localhost","test.htm");
JEditorPane jp = new JEditorPane();
jp.setEditable(false);
jp.addHyperlinkListener(this);
jp.setContentType("text/html");
jp.setPage(url);
JScrollPane scroller = new JScrollPane(jp);
p.add(scroller, BorderLayout.CENTER);
}
catch (Exception e) {
System.out.println("Unexpected error: " + e);
System.exit(0);
}
p.setPreferredSize(new Dimension(300,300));
setContentPane(p);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent w) {
System.exit(0);
}
});
pack();
show();
}
/**
* Notification of a change relative to a hyperlink.
*/
public void hyperlinkUpdate(HyperlinkEvent e) {
HyperlinkEvent.EventType type = e.getEventType();
if (type == HyperlinkEvent.EventType.ACTIVATED) {
JEditorPane pane = (JEditorPane) e.getSource();
if (e instanceof HTMLFrameHyperlinkEvent) {
HTMLFrameHyperlinkEvent evt = (HTMLFrameHyperlinkEvent) e;
HTMLDocument doc = (HTMLDocument)
pane.getDocument();
doc.processHTMLFrameHyperlinkEvent(evt);
} else {
try {
pane.setPage(e.getURL());
} catch(Throwable t) {
t.printStackTrace();
}
}
}
}
public static void main(String args[]) {
HTML html = new HTML();
}
}
// End HTML.java
------------------------------------------------------
test.htm
<html>
<body>
<IMG SRC='test.jpg'>
</body>
</html
Andrew Thompson - 05 Nov 2004 05:13 GMT
Your example works* here when I put test.htm in the same
directory as the application along with a suitably named image.
* The HTML document loads and displays the image.

Signature
Andrew Thompson
http://www.PhySci.org/codes/ Web & IT Help
http://www.PhySci.org/ Open-source software suite
http://www.1point1C.org/ Science & Technology
http://www.LensEscapes.com/ Images that escape the mundane
Rogan Dawes - 05 Nov 2004 10:05 GMT
> I have a user (in a large university environment) that is using an
> application which uses JEditorPane to display reports in HTML format.
[quoted text clipped - 15 lines]
> supply your own jpg (in place of test.jpg). Any help would be greatly
> appreciated. Thanks - Lou
You might want to check out the following article for some background of
how JEditorPane loads images. It might provide some insight into what
you are seeing:
http://www.javaworld.com/javaworld/javatips/jw-javatip109.html
Regards,
Rogan

Signature
Rogan Dawes
*ALL* messages to discard@dawes.za.net will be dropped, and added
to my blacklist. Please respond to "nntp AT dawes DOT za DOT net"
Lou Lipnickey - 05 Nov 2004 14:18 GMT
Did you uncomment the line:
URL url = new URL("file","localhost","test.htm");
(maybe I forgot to mention it)? Thats the one which is causing the
rendering issue. Thanks
Lou
>> I have a user (in a large university environment) that is using an
>> application which uses JEditorPane to display reports in HTML format.
[quoted text clipped - 25 lines]
>
> Rogan
Chris Smith - 07 Nov 2004 15:51 GMT
> I have a user (in a large university environment) that is using an
> application which uses JEditorPane to display reports in HTML format.
[quoted text clipped - 3 lines]
> exactly recall. To get around this problem, I changed the statement to :
> URL("file","localhost","test.htm");
Neither of those are the way I'd do things. Why not try:
URL url = new URL(new File("test.htm").toURI().toURL());
Of course, if "test.htm" is not in the current working directory, then
you'll need to give a full path or specify the base directory as a first
parameter to the File constructor. What the above code solves, though,
is any potential problem you might be having with the format of the
file-scheme URL. The formats can be difficult to keep straight between
various platforms, so why not let the API do it for you?

Signature
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.
Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
Andrew Thompson - 07 Nov 2004 16:39 GMT
> Why not try:
>
> URL url = new URL(new File("test.htm").toURI().toURL());
I expect you meant..
URL url = new File("test.htm").toURI().toURL();
( Probably because that will not compile ;-)
I was about to add 'and that would require a signed applet..'
until I recalled the OP was talking about an application,
whereas I am used to dealing with JEP's in applets, where
"URL's rule!"
(snip)
> The formats can be difficult to keep straight between
> various platforms, so why not let the API do it for you?
A sterling idea!
( Sometimes I will whip up a quick 'main()' with a file object
just so I can see the format of the URL - for the umpteen
dozenth time.. )

Signature
Andrew Thompson
http://www.PhySci.org/codes/ Web & IT Help
http://www.PhySci.org/ Open-source software suite
http://www.1point1C.org/ Science & Technology
http://www.LensEscapes.com/ Images that escape the mundane
Chris Smith - 07 Nov 2004 18:19 GMT
> > Why not try:
> >
> > URL url = new URL(new File("test.htm").toURI().toURL());
>
> I expect you meant..
> URL url = new File("test.htm").toURI().toURL();
Yes, I did. Sorry for the confusion.

Signature
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.
Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation