3
import com.teamdev.jxbrowser.chromium.Browser;
import com.teamdev.jxbrowser.chromium.BrowserFunction;
import com.teamdev.jxbrowser.chromium.JSValue;
import com.teamdev.jxbrowser.chromium.LoggerProvider;
import com.teamdev.jxbrowser.chromium.swing.BrowserView;

import javax.swing.*;
import java.awt.*;
import java.util.logging.Level;
import java.util.logging.Logger;

public class JavaScriptJavaSample {
  public static void main(String[] args) {
    LoggerProvider.setLevel(Level.OFF);
    Browser browser = new Browser();
    BrowserView browserView = new BrowserView(browser);

    browser.registerFunction("MyFunction", new BrowserFunction() {
        public JSValue invoke(JSValue... args) {
            System.out.println("MyFunction is invoked!");
            return JSValue.create("Hello!");
        }
    });

    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    frame.add(browserView, BorderLayout.CENTER);
    frame.setSize(700, 500);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);

    browser.loadHTML("<html><body><a href='#' onclick='MyFunction();'>Call Java method</a></body></html>");
}
}

i've add all jar file,but BrowserFunction doesn't exist, why?

all other classes work perfectly , and do not understand the problem , I

imported all the jar thanks to all in advance

Francesco Taioli
  • 2,687
  • 1
  • 19
  • 34

2 Answers2

2

As far as i know, using BrowserFunction() to register function is gone in new versions of jxbrowser. There is a new way in 6.1 version described here. Putting the new way in your code :

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

import javax.swing.*;
import java.awt.*;
import java.util.logging.Level;
import java.util.logging.Logger;

public class JavaScriptJavaSample {
    public static void main(String[] args) {
        LoggerProvider.setLevel(Level.OFF);
        Browser browser = new Browser();
        BrowserView browserView = new BrowserView(browser);

        browser.addLoadListener(new LoadAdapter() {
            @Override
            public void onFinishLoadingFrame(FinishLoadingEvent event) {
                if (event.isMainFrame()) {
                    Browser browser = event.getBrowser();
                    JSValue value = browser.executeJavaScriptAndReturnValue("window");
                    value.asObject().setProperty("java", new Events());
                }
            }
        });

        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.add(browserView, BorderLayout.CENTER);
        frame.setSize(700, 500);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

        browser.loadHTML("<html><body><a href='#' onclick='alert(java.MyFunction());return false;'>Call Java method</a></body></html>");
    }
}

Create another file to hold Events class :

public class Events {

    public String MyFunction() {

        System.out.println("MyFunction is invoked!");

        return new String("Hello");

    }

}

* Added return false to the end of onclick attribute of a tag, preventing browser from following the link href on click. BTW, I suggest you to use main documentation codes, as they are up-to-date.

Mehran Torki
  • 977
  • 1
  • 9
  • 37
0

In order to use the BrowserFunction you should have the licence.jar and add it to your classpath .
JxBrowser has a free 30day-trial . you can also buy the license .

Shahryar Saljoughi
  • 2,599
  • 22
  • 41