JEditorpane cannot load url
I have a problem with my JEditorPane, can't load url, always show java.io.FileNotFoundException. Overall I am confused how to solve it.
JEditorPane editorpane = new JEditorPane();
editorpane.setEditable(false);
String backslash="\\";
String itemcode="a91000mf";
int ctr=6;
File file = new File("file:///C:/Development/project2/OfflineSales/test/index.html?item_code="+itemcode+"&jumlah="+String.valueOf(ctr)+"&lokasi=../images");
if (file != null) {
try {
//editorpane.addPropertyChangeListener(propertyName, listener)
editorpane.setPage(file.toURL());
System.out.println(file.toString());
} catch (IOException e) {
System.err.println(e.toString());
}
} else {
System.err.println("Couldn't find file: TextSamplerDemoHelp.html");
}
I just put "file:///C:/Development/project2/OfflineSales/test/index.html?item_code="+itemcode"
, but it shows the same error: cannot open the file, but I can open it in my browser
source to share
You must remove all use of the File class.
The line starting with "file:" is a URL, not a file name. This is not a valid argument for a file constructor.
You are calling the JEditor.setPage method , which accepts a URL, not a file. There is no reason to instantiate File:
try {
URL url = new URL("file:///C:/Development/project2/OfflineSales/test/index.html?item_code=" + itemcode + "&jumlah=" + ctr + "&lokasi=../images");
editorpane.setPage(url);
} catch (IOException e) {
e.printStackTrace();
}
JEditorPane also has a convenience method that converts the string to a URL for you, so you can even eliminate the use of the URL class entirely:
String url = "file:///C:/Development/project2/OfflineSales/test/index.html?item_code=" + itemcode + "&jumlah=" + ctr + "&lokasi=../images";
try {
editorpane.setPage(url);
} catch (IOException e) {
e.printStackTrace();
}
(Note that String.valueOf
it is not required. It is implicitly called whenever you concatenate a String with any object or primitive value.)
source to share