Search code examples
javascriptfunctioncalljxbrowser

How to call a javascript function in JXBrowser


I need to upload an internal url to a Swing component and then call a JavaScript function from the loaded html. I'm currently testing the JXBrowser package to see if it fits my demands. For testing simplicity I put the call in a JButton, so I can manually invoke it. unfortunately, while I can generate a simple alert call, I am unable to make the code successfully call the function. here is my code:

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.WindowConstants;

import com.teamdev.jxbrowser.chromium.Browser;
import com.teamdev.jxbrowser.chromium.JSValue;
import com.teamdev.jxbrowser.chromium.swing.BrowserView;

public class JXBrowserTest {

    public static void main(String[] args) {

        final Browser browser = new Browser();
        BrowserView view = new BrowserView(browser);
        String text = "";
        try {
            text = readFile("C:\\work\\temp\\report.txt");
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        final String report = text;
        JButton button = new JButton("Java Script");
        button.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {

                JSValue value = browser.executeJavaScriptAndReturnValue("gotoReport(" +report+")");
                //browser.executeJavaScript("window.alert(\"Nothing is True\")");//this does work!

            }
        });

        JFrame frame = new JFrame("JxBrowser - Dialoge Test");
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);

        frame.add(view, BorderLayout.CENTER);
        frame.add(button, BorderLayout.PAGE_END);
        frame.setSize(500, 400);
        frame.setVisible(true);

        browser.loadURL("http://myInternalUrl.swe");

    }

    private static String readFile(String file) throws IOException {
        BufferedReader reader = new BufferedReader(new FileReader(file));
        String line = null;
        StringBuilder stringBuilder = new StringBuilder();
        String ls = System.getProperty("line.separator");

        try {
            while ((line = reader.readLine()) != null) {
                stringBuilder.append(line);
                stringBuilder.append(ls);
            }

            return stringBuilder.toString();
        } finally {
            reader.close();
        }
    }
}

I need to call gotoReport(report) in JS, where report is an xml text file. I suspect that my problem is not calling this function correctly with the JXBrowser methods but I tried everything I could think of. can someone please help?


Solution

  • I believe there is a syntax error. Assuming that the content of your file is "Text content", you call your JavaScript function in this way:

    gotoReport(Text content);
    

    when it is expected to call it like this:

    gotoReport('Text content');
    

    I'd recommend that you use the following approach:

    JSFunction gotoReport = browser.executeJavaScriptAndReturnValue("gotoReport").asFunction();
    gotoReport.invoke(null, report);