JavaFX | WebView Class

04 Sep, 2018
WebView class is a part of JavaFX. WebView can create and manage a WebEngine and display its content. The associated WebEngine is created automatically at construction time and cannot be changed. WebView manages keyboard and mouse events and automatically adds a scrollbar to the WebView.
Constructor of the class:
WebView(): Creates a new web view object.
Commonly Used Methods:
Below programs illustrate the use of Webview class:
Java Program to create a WebView and load a website and display it on the stage: In this program we will create a WebView named webview . We will extract WebEngine from the WebView by using getEngine() method. Now load a website on the engine by using load() function, we will set the webview to the scene with preferred height and preferred width and add the scene to the stage using setScene() method and display the stage using show() function.// Java Program to create a WebView and load // a website and display it on the stageimport javafx.application.Application;import javafx.scene.Scene;import javafx.scene.control.*;import javafx.scene.layout.*;import javafx.stage.Stage;import javafx.event.ActionEvent;import javafx.event.EventHandler;import javafx.scene.canvas.*;import javafx.scene.web.*;import javafx.scene.Group;   public class SliderExample extends Application {       // launch the application    public void start(Stage stage)    {        try {               // set title for the stage            stage.setTitle("creating Webview");               // create a webview object            WebView w = new WebView();               // get the web engine            WebEngine e = w.getEngine();               // load a website            e.load("https://www.geeksforgeeks.org");               // create a scene            Scene scene = new Scene(w, w.getPrefWidth(),                                      w.getPrefHeight());               // set the scene            stage.setScene(scene);               stage.show();        }           catch (Exception e) {               System.out.println(e.getMessage());        }    }       // Main Method    public static void main(String args[])    {           // launch the application        launch(args);    }}Output:Java Program to create a WebView and load a website, set the font scale, also set the zoom and display it on the stage: In this program we will create a WebView named webview. We will extract WebEngine from the WebView by using getEngine() method. Now set the font size and the zoom of the object using the setFontSize() and setZoom() function. We will load a website on the engine by using function load(). Then set the webview to the scene with preferred height and preferred width and add the scene to the stage using setScene() method and display the stage using show() function.// Java Program to create a WebView and load // a website, set the fontscale, also set // the zoom and display it on the stageimport javafx.application.Application;import javafx.scene.Scene;import javafx.scene.control.*;import javafx.scene.layout.*;import javafx.stage.Stage;import javafx.event.ActionEvent;import javafx.event.EventHandler;import javafx.scene.canvas.*;import javafx.scene.web.*;import javafx.scene.Group;  public class webview_2 extends Application {      // launch the application    public void start(Stage stage)    {          try {              // set title for the stage            stage.setTitle("creating Webview");              // create a webview object            WebView w = new WebView();              // get the web engine            WebEngine e = w.getEngine();              // load a website            e.load("https://www.geeksforgeeks.org");              // set font scale for the webview            w.setFontScale(1.5f);              // set zoom            w.setZoom(0.8);              // create a scene            Scene scene = new Scene(w, w.getPrefWidth(),                                     w.getPrefHeight());              // set the scene            stage.setScene(scene);              stage.show();        }          catch (Exception e) {              System.out.println(e.getMessage());        }    }      // Main Method    public static void main(String args[])    {          // launch the application        launch(args);    }}Output:
Java Program to create a WebView and load a website and display it on the stage: In this program we will create a WebView named webview . We will extract WebEngine from the WebView by using getEngine() method. Now load a website on the engine by using load() function, we will set the webview to the scene with preferred height and preferred width and add the scene to the stage using setScene() method and display the stage using show() function.// Java Program to create a WebView and load // a website and display it on the stageimport javafx.application.Application;import javafx.scene.Scene;import javafx.scene.control.*;import javafx.scene.layout.*;import javafx.stage.Stage;import javafx.event.ActionEvent;import javafx.event.EventHandler;import javafx.scene.canvas.*;import javafx.scene.web.*;import javafx.scene.Group;   public class SliderExample extends Application {       // launch the application    public void start(Stage stage)    {        try {               // set title for the stage            stage.setTitle("creating Webview");               // create a webview object            WebView w = new WebView();               // get the web engine            WebEngine e = w.getEngine();               // load a website            e.load("https://www.geeksforgeeks.org");               // create a scene            Scene scene = new Scene(w, w.getPrefWidth(),                                      w.getPrefHeight());               // set the scene            stage.setScene(scene);               stage.show();        }           catch (Exception e) {               System.out.println(e.getMessage());        }    }       // Main Method    public static void main(String args[])    {           // launch the application        launch(args);    }}Output:
// Java Program to create a WebView and load // a website and display it on the stageimport javafx.application.Application;import javafx.scene.Scene;import javafx.scene.control.*;import javafx.scene.layout.*;import javafx.stage.Stage;import javafx.event.ActionEvent;import javafx.event.EventHandler;import javafx.scene.canvas.*;import javafx.scene.web.*;import javafx.scene.Group;   public class SliderExample extends Application {       // launch the application    public void start(Stage stage)    {        try {               // set title for the stage            stage.setTitle("creating Webview");               // create a webview object            WebView w = new WebView();               // get the web engine            WebEngine e = w.getEngine();               // load a website            e.load("https://www.geeksforgeeks.org");               // create a scene            Scene scene = new Scene(w, w.getPrefWidth(),                                      w.getPrefHeight());               // set the scene            stage.setScene(scene);               stage.show();        }           catch (Exception e) {               System.out.println(e.getMessage());        }    }       // Main Method    public static void main(String args[])    {           // launch the application        launch(args);    }}
Output:
Java Program to create a WebView and load a website, set the font scale, also set the zoom and display it on the stage: In this program we will create a WebView named webview. We will extract WebEngine from the WebView by using getEngine() method. Now set the font size and the zoom of the object using the setFontSize() and setZoom() function. We will load a website on the engine by using function load(). Then set the webview to the scene with preferred height and preferred width and add the scene to the stage using setScene() method and display the stage using show() function.// Java Program to create a WebView and load // a website, set the fontscale, also set // the zoom and display it on the stageimport javafx.application.Application;import javafx.scene.Scene;import javafx.scene.control.*;import javafx.scene.layout.*;import javafx.stage.Stage;import javafx.event.ActionEvent;import javafx.event.EventHandler;import javafx.scene.canvas.*;import javafx.scene.web.*;import javafx.scene.Group;  public class webview_2 extends Application {      // launch the application    public void start(Stage stage)    {          try {              // set title for the stage            stage.setTitle("creating Webview");              // create a webview object            WebView w = new WebView();              // get the web engine            WebEngine e = w.getEngine();              // load a website            e.load("https://www.geeksforgeeks.org");              // set font scale for the webview            w.setFontScale(1.5f);              // set zoom            w.setZoom(0.8);              // create a scene            Scene scene = new Scene(w, w.getPrefWidth(),                                     w.getPrefHeight());              // set the scene            stage.setScene(scene);              stage.show();        }          catch (Exception e) {              System.out.println(e.getMessage());        }    }      // Main Method    public static void main(String args[])    {          // launch the application        launch(args);    }}Output:
// Java Program to create a WebView and load // a website, set the fontscale, also set // the zoom and display it on the stageimport javafx.application.Application;import javafx.scene.Scene;import javafx.scene.control.*;import javafx.scene.layout.*;import javafx.stage.Stage;import javafx.event.ActionEvent;import javafx.event.EventHandler;import javafx.scene.canvas.*;import javafx.scene.web.*;import javafx.scene.Group;  public class webview_2 extends Application {      // launch the application    public void start(Stage stage)    {          try {              // set title for the stage            stage.setTitle("creating Webview");              // create a webview object            WebView w = new WebView();              // get the web engine            WebEngine e = w.getEngine();              // load a website            e.load("https://www.geeksforgeeks.org");              // set font scale for the webview            w.setFontScale(1.5f);              // set zoom            w.setZoom(0.8);              // create a scene            Scene scene = new Scene(w, w.getPrefWidth(),                                     w.getPrefHeight());              // set the scene            stage.setScene(scene);              stage.show();        }          catch (Exception e) {              System.out.println(e.getMessage());        }    }      // Main Method    public static void main(String args[])    {          // launch the application        launch(args);    }}
Output:
Note: The above programs might not run in an online IDE. Please use an offline compiler.
Reference: https://docs.oracle.com/javase/8/javafx/api/javafx/scene/web/WebView.html
JavaFX
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Object Oriented Programming (OOPs) Concept in Java
How to iterate any Map in Java
Interfaces in Java
HashMap in Java with Examples
ArrayList in Java
Collections in Java
Stream In Java
Multidimensional Arrays in Java
Singleton Class in Java
Set in Java