URL urlHello = getClass().getResource(hellohtml);
webEngine.load(urlHello.toExternalForm());
Create a simple local web page, hello.html.
<!DOCTYPE html>
<html lang="en-US">
<head>
<meta charset=utf-8>
<title>Hello Java-Buddy!</title>
</head>
<body>
<img src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjxGfYYk9MPmsy4Fb5yBONPauX59KlswhZvZL_XoWgvPMp2SwHsLorHIo4aWZJsUc9oynuzwstK5MfzjmjmNevnJFjJt1HIhcl-iRH3dnK2twWSeX6MdjSbAz4nyMzaNQE1p-Pajqfg3KKN/s150/duke_44x80.png"/>
<p>Hello <a href="http://java-buddy.blogspot.com/">Java-Buddy</a></p>
</body>
</html>
package javafx_webview;
import java.net.URL;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Region;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
/**
*
* @web http://java-buddy.blogspot.com/
*/
public class JavaFX_Browser extends Application {
private Scene scene;
MyBrowser myBrowser;
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("java-buddy.blogspot.com");
myBrowser = new MyBrowser();
scene = new Scene(myBrowser, 640, 480);
primaryStage.setScene(scene);
primaryStage.show();
}
class MyBrowser extends Region{
final String hellohtml = "hello.html";
WebView webView = new WebView();
WebEngine webEngine = webView.getEngine();
public MyBrowser(){
URL urlHello = getClass().getResource("hello.html");
webEngine.load(urlHello.toExternalForm());
getChildren().add(webView);
}
}
}
Related:
- Generate and load html content dynamically using Java code
- Add toolbar in our browser
could you please explain how to do this with a local image? I can't seem to get the file path correct
ReplyDeleteNevermind, I was dynamically creating the html and found this answer
ReplyDeletehttp://stackoverflow.com/questions/8923801/how-to-reach-css-and-image-files-from-the-html-page-loaded-by-javafx-scene-web-w
Thanks for the great article!
Thanks for posting this code. I find that the html file has to be located in the bin directory when using Eclipse or else the URL object is null. I am writing an application where the UI writes an html file that contains simulation model results. This file is intended to serve as a pre-written report for the model results. Is there a way to read an html file from anywhere on the machines hard drive?
ReplyDelete