How to setPage() a JEditorPane with a localfile which is outside of the .jar file?
Posted
by
Jaguar
on Stack Overflow
See other posts from Stack Overflow
or by Jaguar
Published on 2010-12-26T18:11:22Z
Indexed on
2010/12/26
18:54 UTC
Read the original article
Hit count: 227
java
|jeditorpane
My program has the following path in the .jar file
src/test/Program.class
and my program is as follow...
Program.java
package test;
import java.io.File;
import java.io.IOException;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
public class Program {
JEditorPane editorPane;
public Program() {
File file = new File("temp.htm");
try {
file.createNewFile();
editorPane = new JEditorPane();
editorPane.setPage(Program.class.getResource("temp.htm"));
} catch (IOException e) {
e.printStackTrace();
}
}
public JEditorPane getEditorPane(){
return editorPane;
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 400);
frame.setVisible(true);
Program p = new Program();
frame.getContentPane().add(p.getEditorPane());
}
}
The problem is that I have compiled the program in a .jar
file.
The file.createNewFile();
creates the temp.htm
file outside the .jar
file
So when editorPane.setPage(Program.class.getResource("temp.htm"));
is called the file is not found as it searches for file inside the test
package.
How to setPage()
the temp.htm
file whiich is outside the .jar
file but in the same folder as the .jar
file?
As the temp.htm
is a localfile and I want a relative path instead of an absolute path.
Thanks.
© Stack Overflow or respective owner