Skip to content

Instantly share code, notes, and snippets.

@alamenai
Last active March 27, 2018 17:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alamenai/68e192c6d4708ef3e1d305f9d5df2f3b to your computer and use it in GitHub Desktop.
Save alamenai/68e192c6d4708ef3e1d305f9d5df2f3b to your computer and use it in GitHub Desktop.
JavaFx example to make a changes in html file and reload it again in webview.
public void reloadContent()
{
//Using Jsoup library https://jsoup.org/cookbook/introduction/parsing-a-document
//Get html file
InputStream inputStream = Odata.class.getClass().getResourceAsStream("/web/html/template.html");
//Parse the content
org.jsoup.nodes.Document doc = Jsoup.parse(inputStream, "UTF-8", "");
//Get Element for make some changes
org.jsoup.nodes.Element content = doc.getElementById("name");
//Change the text of element
content.text("Menai Ala Eddine");
//Get image element
Element image = doc.getElementById("profile-image");
//Change relative path in jar file
//The original path in html file :src="../../appimages/images/male-user-profile-picture.png"
image.attr("src",getClass().getResource("/appimages/images/male-user-profile-picture.png").toExternalForm());
WebView browser = new WebView();
WebEngine webEngine = browser.getEngine();
//Load the new content after make changing
webEngine.loadContent(doc.html());
webEngine.setUserStyleSheetLocation(getClass().getResource("/web/stylesheets/inscription_template.css").toString());
Stage embeddedStage = new Stage();
StackPane pane = new StackPane(browser);
Scene embeddedScene = new Scene(pane, 500, 500);
embeddedStage.setScene(embeddedScene);
embeddedStage.show();
}