Updated April 12, 2023
Definition of JavaFX WebView
JavaFX WebView is used to show the web pages, that’s why the name webView here. It acts as a mini or a small browser that is able to represent the pages on the view. It is capable of showing CSS, HTML, and JAVASCRIPT code on the webView, inside the JavaFX application. To use this we need to import the library from the JavaFX while programming. In short, webView is used to render the web pages, It is very easy to use where we can show our different pages like news, blogs and also able to download them very fast. In the next section, we will discuss the WebView in detail.
Syntax:
To use WebView we need to import the library into our program. As we know it is the part of JavaFX package. For more understating see the syntax below;
import javafx.scene.web.WebView;
WebView name_varible = new WebView();
As you can see in the above lines of code first we are importing the package in which webView is present after that we are creating an object of the webView simply using the ‘new’ keyword. Also this class available inside the JavaFX package only. Let’s see one practice syntax for beginners for a better understanding of the syntax see below;
import javafx.scene.web.WebView;
WebView myview = new WebView();
// logic to call methods
Here we are just assigning the name for the variable and after this, we can call any method which is available inside the web view class to handle the web pages in JavaFX application
How JavaFX WebView works?
As now we know that WebView is used to show our web pages in the JavaFX application. This is very lightweight and can be easily used. By using this WebView we can render our HTML pages on the web view. This also supports CSS and JavaScript. This module is the node for the JavaFX application so we need to import this while using in the program. Also internally WebView uses a web-kit to render our web pages on the mini or a small browser. Let’s see and understand the internal working of the WebView node in JavaFX see below;
e.g. :
import javafx.scene.web.WebView;
String myText = "Demo to show internal working of webview";
WebView myview = new WebView();
myview.loadContent(myText, "file.html");
In the above lines of code, we are trying to show the text into the HTML file using WebView in JavaFX application. For this, we have created an instance of the WebView first after this e are calling loadContent() method of the WebView node to show the text on the HTML file. This loadContent() method takes 2 parameters inside it. First is the text that you want to show and another one is the file name on which we want to print this text. After this we can easily able to see our text on the HTML page.
Constructors
WebView is a node and in order to use this node, we need to have an object of this Class. After the object creation only we are able to use its method inside the program. Also in order to create the object in an object-oriented programming language, we need a constructor. In WebView w have only one constructor as the instance method for the WebView, by the use of it we can initialize it easily. Let’s discuss its usages in JavaFX application see below;
Syntax:
WebView myview = new WebView();
This is the default constructor or no parameter constructor in WebView to initialize the object of WebView. In order to create the object new use ‘new’ keyword here. After this we are able to access is methods and other variables present inside the WebView class.
Methods
WebView provides a number of methods to deal with the web pages in JavaFX application. Let’s discuss each of the methods in detail which are as follows see below:
1) width: This method is used to set the width for our webview.
2) maxHeight: This method is used to set the maximum height for the webview node in JavaFX application
3) height: This method is used to set the height for the WebView node in JavaFX application.
4) maxWidth: This method is used to set the maximum width for the WebView node in JavaFX application.
5) zoom: This method is used to get the zoom object in webview.
6) prefWidth: This method is used to set the preferred width for the WebView node in JavaFX application.
7) prefHeight: This method is used to set the preferred height for the WebView node in JavaFX application.
8) min-width: This method is used to set the minimum width for the WebView node in JavaFX application.
9) minHeight: This method is used to set the minimum height for the WebView node in JavaFX application
10) fontScale: This method is used to specify the font scale for the font in WebView.
11) contextMenuEnabled: This method used to check context menu is enabled or not.
Example of JavaFX WebView
In this example, we are creating a window and inside it, we are trying to open the google link.
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.VBox;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
import javafx.stage.Stage;
import javafx.scene.web.WebEngine;
public class DemoWebView extends Application {
public static void main(String[] args) {
launch(args);
}
public void demo
(Stage mystage) {
System.out.println("Demo for webview");
//webview object creation
//web engine object creation
WebView myview = new WebView();
// setting min height
myview.minHeight(1050);
// setting preferred width
myview.prefWidth(1950);
// setting pref heigth
myview.prefHeight(1070);
//setting min width
myview.minWidth(1050);
final WebEngine mywebEngine = webView.getEngine();
mywebEngine.load("http://www.google.com");
VBox mybox = new VBox(webView);
Scene myscene = new Scene(mybox, 960, 600);
mystage.setScene(myscene);
mystage.show();
}
}
Output:
Conclusion
It can be used to display or render our web pages, This acts as a mini browser also it uses web kit internally to show our web pages. This is a node inside the JavaFX package also by using WebView we can show web pages in JavaFX application only.
Recommended Articles
This is a guide to JavaFX WebView. Here we also discuss the definition, methods, How JavaFX WebView works with example. You may also have a look at the following articles to learn more –