JavaFX 2 WebView URL Listener

java, javafx-2

Solution

Create your own URL protocol handler and install it using:

URL.setURLStreamHandlerFactory(new HandlerFactory());

A New Era for Java URL Protocol Handlers article provides detailed instructions for creating the handler.

WebView can make use of your custom protocol to load your content.

Problem

When using a JavaFX (2.2) WebView, is there a way to listen to and handle url's within java code? For example: I'm loading a local HTML file to my WebView with webEngine.loadContent(html). The HTML contains resources like ``` <script src="local:my-script.js"></script> <img src="cms:1234.png"/> ``` which is also a local file and should be provided from the java application. So I want to register a listener that could handle requests from the page. Edit: The resource data that is loaded within the HTML page comes from a content-managemant-system so using relative paths is not working.

Original source