These are simple step by step code snippets to allow you learn JavaFX.

Example 1: Webview

Study the following code:

Step 1: Create Project

  1. Open your favorite Java IDE.
  2. In the menu go to File --> Create New Project.

Step 2: Dependencies

No dependencies are needed for this project.

Step 3: Write Code

Our code will comprise the following java files:

  • WebViewExample.java
  • WebViewJavaScriptIntegrationExample.java
  • WebViewMouseWheelZoomExample.java
  1. In your editor or IDE, create a file known as WebViewExample.java.
  2. Then add the following code:

(a). WebViewExample.java

After creating our class, the first thing is to define imports. Such imports are ready made classes that inject more functionalities into our project.

import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.ObservableList;
import javafx.concurrent.Worker;
import javafx.scene.Scene;
import javafx.scene.layout.VBox;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebHistory;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
import netscape.javascript.JSObject;
import org.w3c.dom.Document;

After creating our class, the first thing is to define imports. Such imports are ready made classes that inject more functionalities into our project.

import java.util.Date;
import java.util.Iterator;

Through inheritance we will be able to derive properties from a parent class. However we have to extend that parent class. So we do that using the extends keyword.

public class WebViewExample extends Application {

Our class will have the following methods:

  • void main(String[] args)
  • void start(Stage primaryStage)
  • void historyExamples(WebEngine webEngine)

For our Java program to run we need a main method. Add it as shown below:

     public static void main(String[] args) {

In this particular class we will be overriding our void start(Stage primaryStage) method.
Prepend the code>@Override</code modifier to your method. Then add implementation code as follows:

    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("JavaFX WebView Example");

        WebView webView = new WebView();
        //webView.setZoom(2.25);
        //webView.setFontScale(1.25);
        //webView.setContextMenuEnabled(false);

        WebEngine webEngine = webView.getEngine();
        //webEngine.setUserStyleSheetLocation("assets/webview/stylesheet.css");

        //webEngine.load("http://tutorials.jenkov.com");
        webEngine.loadContent("<!DOCTYPE html><html><body>Hello World!</body></html>", "text/html");

        webEngine.setUserAgent("MyApp Web Browser 1.0");

        //webEngine.reload();

        //historyExamples(webEngine);

        VBox vBox = new VBox(webView);
        Scene scene = new Scene(vBox, 960, 600);

        primaryStage.setScene(scene);
        primaryStage.show();

    }

Here is the full code:

package com.jenkov.javafx.webview;

import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.ObservableList;
import javafx.concurrent.Worker;
import javafx.scene.Scene;
import javafx.scene.layout.VBox;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebHistory;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
import netscape.javascript.JSObject;
import org.w3c.dom.Document;

import java.util.Date;
import java.util.Iterator;

public class WebViewExample extends Application {

     public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("JavaFX WebView Example");

        WebView webView = new WebView();
        //webView.setZoom(2.25);
        //webView.setFontScale(1.25);
        //webView.setContextMenuEnabled(false);

        WebEngine webEngine = webView.getEngine();
        //webEngine.setUserStyleSheetLocation("assets/webview/stylesheet.css");

        //webEngine.load("http://tutorials.jenkov.com");
        webEngine.loadContent("<!DOCTYPE html><html><body>Hello World!</body></html>", "text/html");

        webEngine.setUserAgent("MyApp Web Browser 1.0");

        //webEngine.reload();

        //historyExamples(webEngine);

        VBox vBox = new VBox(webView);
        Scene scene = new Scene(vBox, 960, 600);

        primaryStage.setScene(scene);
        primaryStage.show();

    }

    private void historyExamples(WebEngine webEngine) {
        WebHistory history = webEngine.getHistory();

        ObservableList<WebHistory.Entry> entries = history.getEntries();

        Iterator<WebHistory.Entry> iterator = entries.iterator();
        while(iterator.hasNext()){
            WebHistory.Entry entry = iterator.next();
        }
        for(WebHistory.Entry entry : entries){
            //do something with the entry
            String url           = entry.getUrl();
            String title         = entry.getTitle();
            Date lastVisitedDate = entry.getLastVisitedDate();
        }

        history.go(1);
        history.go(-1);

        int currentIndex = history.getCurrentIndex();
    }
}
  1. Next create another file known as WebViewJavaScriptIntegrationExample.java.
  2. And add the following code:

(b). WebViewJavaScriptIntegrationExample.java

After creating our class, the first thing is to define imports. Such imports are ready made classes that inject more functionalities into our project.

import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.ObservableList;
import javafx.concurrent.Worker;
import javafx.scene.Scene;
import javafx.scene.layout.VBox;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebHistory;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
import netscape.javascript.JSObject;
import org.w3c.dom.Document;

After creating our class, the first thing is to define imports. Such imports are ready made classes that inject more functionalities into our project.

import java.util.Date;
import java.util.Iterator;

Through inheritance we will be able to derive properties from a parent class. However we have to extend that parent class. So we do that using the extends keyword.

public class WebViewJavaScriptIntegrationExample extends Application {

Our class will have the following methods:

  • void doIt()
  • void main(String[] args)
  • void start(Stage primaryStage)
  • void changed(ObservableValue observable, Object oldValue, Object newValue)

For our Java program to run we need a main method. Add it as shown below:

    public static void main(String[] args) {

We have 2methods to override in this class. Here are those methods:

  • public void start(Stage primaryStage) {
  • public void changed(ObservableValue observable, Object oldValue, Object newValue) {
    Prepend the @Override modifier to your method. Then add implementation code as follows:
    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("JavaFX WebView Example");

        WebView webView = new WebView();
        //webView.setZoom(2.25);
        webView.setFontScale(1.25);
        webView.setContextMenuEnabled(false);

        WebEngine webEngine = webView.getEngine();
        //webEngine.setUserStyleSheetLocation("assets/webview/stylesheet.css");

        webEngine.loadContent("<!DOCTYPE html><html><body>Hello World!<script type="text/javascript">function hmm() { return 'Hey!'; }; document.write(hmm()); </script><button onclick='window.myObject.doIt();'>Do It</button></body></html>", "text/html");

        webEngine.setUserAgent("MyApp Web Browser 1.0");

        webEngine.getLoadWorker().stateProperty().addListener(
            new ChangeListener() {
                @Override
                public void changed(ObservableValue observable, Object oldValue, Object newValue) {
                    System.out.println("oldValue: " + oldValue);
                    System.out.println("newValue: " + newValue);

                    if (newValue != Worker.State.SUCCEEDED) {
                        return;
                    }

Here is the full code:

package com.jenkov.javafx.webview;

import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.ObservableList;
import javafx.concurrent.Worker;
import javafx.scene.Scene;
import javafx.scene.layout.VBox;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebHistory;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
import netscape.javascript.JSObject;
import org.w3c.dom.Document;

import java.util.Date;
import java.util.Iterator;

public class WebViewJavaScriptIntegrationExample extends Application {

    public static class MyObject {

        public void doIt() {
            System.out.println("doIt() called");
        }
    }

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("JavaFX WebView Example");

        WebView webView = new WebView();
        //webView.setZoom(2.25);
        webView.setFontScale(1.25);
        webView.setContextMenuEnabled(false);

        WebEngine webEngine = webView.getEngine();
        //webEngine.setUserStyleSheetLocation("assets/webview/stylesheet.css");

        webEngine.loadContent("<!DOCTYPE html><html><body>Hello World!<script type="text/javascript">function hmm() { return 'Hey!'; }; document.write(hmm()); </script><button onclick='window.myObject.doIt();'>Do It</button></body></html>", "text/html");

        webEngine.setUserAgent("MyApp Web Browser 1.0");

        webEngine.getLoadWorker().stateProperty().addListener(
            new ChangeListener() {
                @Override
                public void changed(ObservableValue observable, Object oldValue, Object newValue) {
                    System.out.println("oldValue: " + oldValue);
                    System.out.println("newValue: " + newValue);

                    if (newValue != Worker.State.SUCCEEDED) {
                        return;
                    }
                    System.out.println("Succeeded!");
                    String hello = (String) webEngine.executeScript("hmm()");
                    System.out.println("hello: " + hello);

                    JSObject window = (JSObject) webEngine.executeScript("window");
                    window.setMember("myObject", new MyObject());

                    Document document = webEngine.getDocument();
                }
            }
        );

        String result = (String) webEngine.executeScript("hmm()");
        System.out.println(result);

        VBox vBox = new VBox(webView);
        Scene scene = new Scene(vBox, 960, 600);

        primaryStage.setScene(scene);
        primaryStage.show();

    }

}
  1. Next create another file known as WebViewMouseWheelZoomExample.java.
  2. And add the following code:

(c). WebViewMouseWheelZoomExample.java

After creating our class, the first thing is to define imports. Such imports are ready made classes that inject more functionalities into our project.

import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.ObservableList;
import javafx.concurrent.Worker;
import javafx.scene.Scene;
import javafx.scene.input.ScrollEvent;
import javafx.scene.layout.VBox;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebHistory;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
import netscape.javascript.JSObject;
import org.w3c.dom.Document;

After creating our class, the first thing is to define imports. Such imports are ready made classes that inject more functionalities into our project.

import java.util.Date;
import java.util.Iterator;

Through inheritance we will be able to derive properties from a parent class. However we have to extend that parent class. So we do that using the extends keyword.

public class WebViewMouseWheelZoomExample extends Application {

Our class will have the following methods:

  • void doIt()
  • void main(String[] args)
  • void start(Stage primaryStage)

For our Java program to run we need a main method. Add it as shown below:

    public static void main(String[] args) {

In this particular class we will be overriding our void start(Stage primaryStage) method.
Prepend the code>@Override</code modifier to your method. Then add implementation code as follows:

    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("JavaFX WebView Example");

        WebView webView = new WebView();

        WebEngine webEngine = webView.getEngine();

        //webEngine.load("http://tutorials.jenkov.com");
        webEngine.loadContent("<!DOCTYPE html><html><body>Hello World!<script type="text/javascript">function hmm() { return 'Hey!'; }; document.write(hmm()); </script><button onclick='window.myObject.doIt();'>Do It</button></body></html>", "text/html");

        webView.addEventFilter(ScrollEvent.SCROLL, (ScrollEvent e) -> {
            double deltaY = e.getDeltaY();
            if (e.isControlDown() && deltaY > 0) {
                webView.setZoom(webView.getZoom() * 1.1);
                e.consume();
            } else if (e.isControlDown() && deltaY < 0) {
                webView.setZoom(webView.getZoom() / 1.1);
                e.consume();
            }

Here is the full code:

package com.jenkov.javafx.webview;

import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.ObservableList;
import javafx.concurrent.Worker;
import javafx.scene.Scene;
import javafx.scene.input.ScrollEvent;
import javafx.scene.layout.VBox;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebHistory;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
import netscape.javascript.JSObject;
import org.w3c.dom.Document;

import java.util.Date;
import java.util.Iterator;

/**
 * This WebView example shows how to enable a zoom function by
 * scrolling the mouse wheel while holding CTRL down. The mouse wheel zoom code
 * was provided by Friedhold Matz @FriedholdMatz :

        // --- CTRL-Scroll Zooming ---
        webView.addEventFilter(ScrollEvent.SCROLL, (ScrollEvent e) -> {
            double deltaY = e.getDeltaY();
            if (e.isControlDown() && deltaY > 0) {
                webView.setZoom(webView.getZoom() * 1.1);
                e.consume();
            } else if (e.isControlDown() && deltaY < 0) {
                webView.setZoom(webView.getZoom() / 1.1);
                e.consume();
            }
        });
 */

public class WebViewMouseWheelZoomExample extends Application {

    public static class MyObject {

        public void doIt() {
            System.out.println("doIt() called");
        }
    }

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("JavaFX WebView Example");

        WebView webView = new WebView();

        WebEngine webEngine = webView.getEngine();

        //webEngine.load("http://tutorials.jenkov.com");
        webEngine.loadContent("<!DOCTYPE html><html><body>Hello World!<script type="text/javascript">function hmm() { return 'Hey!'; }; document.write(hmm()); </script><button onclick='window.myObject.doIt();'>Do It</button></body></html>", "text/html");

        webView.addEventFilter(ScrollEvent.SCROLL, (ScrollEvent e) -> {
            double deltaY = e.getDeltaY();
            if (e.isControlDown() && deltaY > 0) {
                webView.setZoom(webView.getZoom() * 1.1);
                e.consume();
            } else if (e.isControlDown() && deltaY < 0) {
                webView.setZoom(webView.getZoom() / 1.1);
                e.consume();
            }
        });

        VBox vBox = new VBox(webView);
        Scene scene = new Scene(vBox, 960, 600);

        primaryStage.setScene(scene);
        primaryStage.show();

    }

}

Download

Download the code using the below links:

Number Link
1. Download Example
2. Follow code author
3. Code: Apache 2.0 License