java - How to open local text file with JavaFX WebView -
is possible open local text file javafx webview ? have try following code not working. how can make enable this? thanks.
webview wv = new webview(); wv.getengine().setcreatepopuphandler(new callback<popupfeatures, webengine>() { @override public webengine call(popupfeatures p) { stage stage = new stage(stagestyle.utility); webview wv2 = new webview(); stage.setscene(new scene(wv2)); stage.show(); return wv2.getengine(); } }); wv.getengine().loadcontent("<a href="file:///c:\users\dev\infor.txt">open file</a>"); stackpane root = new stackpane(); root.getchildren().add(wv); scene scene = new scene(root, 300, 250); primarystage.settitle("hello world!"); primarystage.setscene(scene); primarystage.show();
yes, can open local text file javafx webview.
sample app:
import javafx.application.application; import javafx.scene.scene; import javafx.scene.web.webview; import javafx.stage.stage; public class webviewwithlocaltext extends application { @override public void start(stage stage) throws malformedurlexception { string location = new file( system.getproperty("user.dir") + file.separator + "test.txt" ).touri().tourl().toexternalform(); system.out.println(location); webview webview = new webview(); webview.getengine().load(location); // use loadcontent instead of load if want link file. // webview.getengine().loadcontent( // "<a href=\"" + location + "\">open file</a>" // ); scene scene = new scene(webview); stage.setscene(scene); stage.show(); } public static void main(string[] args) { launch(args); } }
place text file @ location reported system.out when run program.
sample output:
some errors in supplied code:
- you don't escape quotes.
- you don't supply valid file uri, supply windows path prefixed file protocol.
- you hardcode path , drive specifier, not portable solution between systems.
i don't have windows machine test, perhaps work absolute path.
wv.getengine().loadcontent("<a href=\"file:///c:/users/dev/infor.txt\">open file</a>");
see also:
Comments
Post a Comment