FreeCode

Donate HTML/CSS JavaFX Java Home
JavaFX Starter Template JavaFX Labels JavaFX Buttons JavaFX Toggle Buttons JavaFX Radio Buttons and Toggle Groups JavaFX Checkboxes JavaFX Choice Boxes JavaFX Combo Boxes JavaFX Text Areas JavaFX Text Fields JavaFX List Views JavaFX Image Views JavaFX Color Pickers JavaFX Date Pickers JavaFX Resizable Split Pane Layouts JavaFX VBox (Vertical Box Layouts) JavaFX HBox (Horizontal Box Layouts) JavaFX Flow Pane Layouts JavaFX Tile Pane Layouts JavaFX Web Views

JavaFX Web Views

Code Description:

The code below shows you how to make a Web View which has the ability to search and display websites or display raw HTML content. In our runnable example, we first load up the Web View with a website URL. When the button is clicked, we load raw HTML content instead. Web Views tie in with the WebEngine Class as well.

Web View Syntax:

WebView webView = new WebView();
WebEngine webEngine = webView.getEngine();
		
webEngine.load("https://www.freecode.net/");

Runnable JavaFX Web View Example Code:

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.VBox;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;

public class Demo extends Application {

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

    @Override
    public void start(Stage primaryStage) throws Exception {

	VBox root = new VBox();
		
	WebView webView = new WebView();
	WebEngine webEngine = webView.getEngine();
		
	webEngine.load("https://www.freecode.net/");
		
	Button button = new Button("Press to load raw HTML content");
		
	button.setOnAction(new EventHandler<ActionEvent>() {
			
	    @Override
	    public void handle(ActionEvent event) {
		webEngine.loadContent("<h1 style=\"color: green;\">Heading</h1>" 
					        + "<p>Paragraph of things<p/>"
						+ "<hr><hr><hr>");
	    }
	});

	root.getChildren().addAll(webView, button);
			
	primaryStage.setTitle("Web View Demo");
	primaryStage.setScene(new Scene(root, 500, 500));
	primaryStage.show();
		
    }

}

How to Run This Code:

Make sure that your Class name is the same as ours. In this case, we are using the Class name of "Demo". Alternatively, you can change the Class name in the provided code to match yours.

Note: You will NOT be able to copy and paste this code into your IDE unless you first set up JavaFX for your project. Check out this link if you need help setting up JavaFX:

JavaFX Comprehensive Setup Guide

Output:

Website Loaded Web View

JavaFX Website Loaded Web View Output Image

Raw HTML Loaded Web View

JavaFX Raw HTML Loaded Web View Output Image

Important Methods:

WebView Class

  • getEngine() - Returns the web view's engine. This is used to associate the web view and the web engine.
  • getZoom() - Returns the current zoom level of the displayed contents.
  • setZoom() - Sets the zoom level of the displayed contents.
  • setPrefSize() - Set the preferred width and height for the web view.
  • setVisible() - Show or hide a web view from the scene. EX: "webView.setVisible(false);"

WebEngine Class

  • loadContent() - Loads the specified raw HTML string.
  • load() - Loads the specified URL.

Related Links:

JavaFX List Views JavaFX Image Views