dcsimg
Problems displaying a html document extracted from a jar file in JEditorPane
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Heiko_Apel
Posted On:   Thursday, April 25, 2002 07:19 AM

I am trying to read html files from jar-archives and display them in a JEditorPane. I am using the follwowing code, but the pages are not shown in the JEditorPane: JEditorPane HelpPane = new JEditorPane(); HTMLEditorKit htmlKit = new HTMLEditorKit(); HelpPane.setEditorKit(htmlKit); HelpPane.setContentType("text/html"); try { JarFile jar = new JarFile("e:/java/phytophthora/lateblight/classes/PhytMod.jar"); JarEntry jarE = new JarEntry(jar.getEntry("lateblight/docs/help.html")); if (jarE != null) { BufferedReader bf = new BufferedReader(new InputStreamReader(jar.getInputStream(jarE))); long size = jarE.getSize(); int chars_read =    More>>

I am trying to read html files from jar-archives and display them in a JEditorPane.
I am using the follwowing code, but the pages are not shown in the JEditorPane:

			
JEditorPane HelpPane = new JEditorPane();
HTMLEditorKit htmlKit = new HTMLEditorKit();
HelpPane.setEditorKit(htmlKit);
HelpPane.setContentType("text/html");
try {
JarFile jar = new JarFile("e:/java/phytophthora/lateblight/classes/PhytMod.jar");
JarEntry jarE = new JarEntry(jar.getEntry("lateblight/docs/help.html"));
if (jarE != null) {
BufferedReader bf = new BufferedReader(new InputStreamReader(jar.getInputStream(jarE)));
long size = jarE.getSize();
int chars_read = 0;
char[] chars = new char[(int) size];
while(bf.ready()) {
chars_read += bf.read(chars, chars_read, (int)size - chars_read);
}
bf.close();
text = new String(chars);
HelpPane.setText(text); //or HelpPane = new JEditorPane("text/html", text); also doesn't work
}
}
catch (IOException ex) {
//exception handling...
}


If I extract the files manually and use HelpPane.setPage(URL) instead, it works perfectly. Also
What's wrong???    <<Less

Re: Problems displaying a html document extracted from a jar file in JEditorPane

Posted By:   Mike_Friedrich  
Posted On:   Friday, April 26, 2002 01:39 AM

What is exactly happen in your case? Is the text correctly read?


Try to use this:


ClassLoader loader = Thread.currentThread().getContextClassLoader();
// PhytMod.jar must be in the classpath
java.net.URL url = loader.getResource("jar:resource:PhytMod.jar!lateblight/docs/help.html");
// if in the current jar: java.net.URL url = loader.getResource("lateblight/docs/help.html");
HelpPane.setPage(url);

This is untested (but i think it should work) and you could get a security exception in an applet.

Regards.
About | Sitemap | Contact