Open In App

JavaFX | WebView Class

Improve
Improve
Like Article
Like
Save
Share
Report

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:

Method Explanation
getChildren() Gets the list of children of this Parent.
getEngine() Returns the engine of the webview.
getFontScale() Returns the fontscale of the webview object.
getHeight() Returns height of this WebView.
getMaxHeight() Returns maximum height.
getMaxWidth() Returns maximum width.
getMinHeight() Sets minimum height.
getMinWidth() Returns minimum width.
getPrefHeight() Returns preferred height.
getPrefWidth() Returns preferred width.
getWidth() Returns width of this WebView.
getZoom() Returns current zoom factor.
maxHeight(double v) Sets the max height.
maxWidth(double v) Sets the max width.
minHeight(double v) Sets the min height.
minWidth(double v) Sets the min width.
prefHeight(double v) Sets the preferred height of the webview.
prefWidth(double v) Sets the preferred width of the webview.
setFontScale(double v) Sets the font scale of the webview.
setMaxHeight(double v) Set the maximum height.
setMaxWidth(double v) Set the maximum width.
setMinHeight(double v) Set the minimum height.
setMinWidth(double v) Set the minimum width.
setPrefHeight(double v) Sets the preferred height.
setPrefWidth(double v) Sets the preferred width.
setZoom(double v) Sets the zoom for the webview.

Below programs illustrate the use of Webview class:

  1. 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 stage
    import 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:

  2. 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 stage
    import 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



Last Updated : 04 Sep, 2018
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads