0

I need to be able to Open my application, input data via TextFields, send said data through FreeMarker and generate HTML, then display in WebView.

Here is where I'm falling off.

I have it all working however I cannot, for the life of me, figure out how to refresh the WebView to display the NEW current HTML doc after data input and rewrite.

It's overwriting the file fine, if I close and reopen, then re click my button, the WebView now shows the new data.

I'm sure the issue is that I'm using getClass().getResource(String):

URL link = getClass().getResource("EmailSigTest.html");
engine = webView.getEngine();
engine.load(link.toString());

How can I get this thing to change dynamically, i.e:

1. Input data
2. Write File
3. Refresh WebView to reflect new html doc

Where do I need to write and read the file to and from to make this happen?

I have tried engine.reload(); to reload the WebEngine... nothing.

I have tried adding an ActionEvent and .reload()... nothing.

Full Source Below:

public class SignatureGenFXMLDocController implements Initializable {
    private String firstName, lastName, directLine, title, cellPhone, lineOne, lineTwo;
    boolean social = false;
    @FXML private TextField txtFirstName, txtLastName, txtDirectLine, txtTitle, txtCellPhone;
    @FXML private CheckBox chkSocialIcons;
    @FXML Button btnGenerate;
    @FXML WebView webView;
    @FXML WebEngine engine;

    @FXML private void handleButtonAction(ActionEvent event) throws InterruptedException {
        System.out.println("You clicked me!");
        firstName = txtFirstName.getText();
        lastName = txtLastName.getText();
        directLine = txtDirectLine.getText();
        title = txtTitle.getText();
        cellPhone = txtCellPhone.getText();

        writeHtml();
        readHtml();
    }

    @Override
    public void initialize(URL url, ResourceBundle rb) {

    }
    private void writeHtml(){
        // Configurae Freemarker
    Configuration cfg = new Configuration();
    try {
        // Load the template
        Template template = cfg.getTemplate("src/signaturegen/template.ftl");
        Map<String, Object> data = new HashMap<String, Object>();

            data.put("fName", firstName);
            data.put("lName", lastName);
            data.put("title", title);
            data.put("dLine", directLine);
            data.put("social", social);

            Writer console = new OutputStreamWriter(System.out);
            template.process(data, console);
            console.flush();
            // File output
            Writer file = new FileWriter (new File("build/classes/signaturegen/EmailSigTest.html"));
            template.process(data, file);
            file.flush();
            file.close();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (TemplateException e) {
            e.printStackTrace();
        }
    }     
    private void readHtml(){
        URL link = getClass().getResource("EmailSigTest.html");
        engine = webView.getEngine();
        engine.load(link.toString());
    }
}
TT.
  • 15,774
  • 6
  • 47
  • 88
NSTuttle
  • 3,237
  • 2
  • 17
  • 26

1 Answers1

1

As discussed in this question, reloading the same URL in the WebView does not work.

What you might want to try is delegating the reload to JavaScript in the page itself. For that you define a function in your page:

function reloadMe() {
  location.reload();
}

And call this function from Java:

private void readHtml(){
    URL link = getClass().getResource("EmailSigTest.html");
    engine = webView.getEngine();
    engine.executeScript("reloadMe();"); 
}

Further reading:

Community
  • 1
  • 1
hotzst
  • 7,238
  • 9
  • 41
  • 64