JavaFX | WebView Class
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:
- 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 stageimportjavafx.application.Application;importjavafx.scene.Scene;importjavafx.scene.control.*;importjavafx.scene.layout.*;importjavafx.stage.Stage;importjavafx.event.ActionEvent;importjavafx.event.EventHandler;importjavafx.scene.canvas.*;importjavafx.scene.web.*;importjavafx.scene.Group;publicclassSliderExampleextendsApplication {// launch the applicationpublicvoidstart(Stage stage){try{// set title for the stagestage.setTitle("creating Webview");// create a webview objectWebView w =newWebView();// get the web engineWebEngine e = w.getEngine();// load a website// create a sceneScene scene =newScene(w, w.getPrefWidth(),w.getPrefHeight());// set the scenestage.setScene(scene);stage.show();}catch(Exception e) {System.out.println(e.getMessage());}}// Main Methodpublicstaticvoidmain(String args[]){// launch the applicationlaunch(args);}}chevron_rightfilter_noneOutput:
-
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 stageimportjavafx.application.Application;importjavafx.scene.Scene;importjavafx.scene.control.*;importjavafx.scene.layout.*;importjavafx.stage.Stage;importjavafx.event.ActionEvent;importjavafx.event.EventHandler;importjavafx.scene.canvas.*;importjavafx.scene.web.*;importjavafx.scene.Group;publicclasswebview_2extendsApplication {// launch the applicationpublicvoidstart(Stage stage){try{// set title for the stagestage.setTitle("creating Webview");// create a webview objectWebView w =newWebView();// get the web engineWebEngine e = w.getEngine();// load a website// set font scale for the webvieww.setFontScale(1.5f);// set zoomw.setZoom(0.8);// create a sceneScene scene =newScene(w, w.getPrefWidth(),w.getPrefHeight());// set the scenestage.setScene(scene);stage.show();}catch(Exception e) {System.out.println(e.getMessage());}}// Main Methodpublicstaticvoidmain(String args[]){// launch the applicationlaunch(args);}}chevron_rightfilter_noneOutput:

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
Recommended Posts:
- JavaFX | Tab Class
- JavaFX | Pos Class
- JavaFX | ColorInput Class
- JavaFX | Rectangle2D Class
- JavaFX | ColorAdjust Class
- JavaFX | SepiaTone Class
- JavaFX | SplitPane Class
- JavaFX | TitledPane Class
- JavaFX | ClosePath class
- JavaFX | Popup Class
- JavaFX | InnerShadow Class
- JavaFX | ToggleButton Class
- JavaFX | Insets Class
- JavaFX | Dimension2D Class
- JavaFX | Bloom Class
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



