JavaFX WebView Browser


package webviewbrowser;

import java.util.List;
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.HPos;
import javafx.geometry.Insets;
import javafx.geometry.VPos;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.layout.ColumnConstraints;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Pane;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;

public class WebViewBrowser extends Application {

    @Override public void start(Stage primaryStage) throws Exception {
        Pane root = new WebViewPane();
        primaryStage.setScene(new Scene(root, 1024, 768));;
    public static void main(String[] args) {

     * Create a resizable WebView pane
    public class WebViewPane extends Pane {

        public WebViewPane() {
            VBox.setVgrow(this, Priority.ALWAYS);

            WebView view = new WebView();
            view.setMinSize(500, 400);
            view.setPrefSize(500, 400);
            final WebEngine eng = view.getEngine();
            final TextField locationField = new TextField("");
            Button goButton = new Button("Go");
            EventHandler<ActionEvent> goAction = new EventHandler<ActionEvent>() {
                @Override public void handle(ActionEvent event) {
                    eng.load(locationField.getText().startsWith("http://") ? locationField.getText() :
                            "http://" + locationField.getText());
            eng.locationProperty().addListener(new ChangeListener<String>() {
                @Override public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
            GridPane grid = new GridPane();
            GridPane.setConstraints(locationField, 0, 0, 1, 1, HPos.CENTER, VPos.CENTER, Priority.ALWAYS, Priority.SOMETIMES);
            GridPane.setConstraints(view, 0, 1, 2, 1, HPos.CENTER, VPos.CENTER, Priority.ALWAYS, Priority.ALWAYS);
                    new ColumnConstraints(100, 100, Double.MAX_VALUE, Priority.ALWAYS, HPos.CENTER, true),
                    new ColumnConstraints(40, 40, 40, Priority.NEVER, HPos.CENTER, true)
            grid.getChildren().addAll(locationField, goButton, view);

        @Override protected void layoutChildren() {
            List<Node> managed = getManagedChildren();
            double width = getWidth();
            double height = getHeight();
            double top = getInsets().getTop();
            double right = getInsets().getRight();
            double left = getInsets().getLeft();
            double bottom = getInsets().getBottom();
            for (int i = 0; i < managed.size(); i++) {
                Node child = managed.get(i);
                layoutInArea(child, left, top,
                               width - left - right, height - top - bottom,
                               0, Insets.EMPTY, true, true, HPos.CENTER, VPos.CENTER);

This is my code. Can someone help me to add a "Stop" Button to stop loading page, add tab to load multiple page and install Adobe Flash Player to my browser so that I can access Youtube(

Thank for helping me.

My am using Netbeans. When i run my application, this is the result.

ant -f C:\\Users\\user\\Documents\\NetBeansProjects\\WebViewBrowser jfxsa-run
Deleting: C:\Users\user\Documents\NetBeansProjects\WebViewBrowser\build\
Updating property file: C:\Users\user\Documents\NetBeansProjects\WebViewBrowser\build\
Detected JavaFX Ant API version 1.2
Launching <fx:jar> task from C:\Program Files\Java\jdk1.7.0_09\lib\ant-javafx.jar
Launching <fx:deploy> task from C:\Program Files\Java\jdk1.7.0_09\lib\ant-javafx.jar
Device "Intel(R) HD Graphics Family" (\\.\DISPLAY1) initialization failed : 
WARNING: bad driver version detected, device disabled. Please update your driver to at least version 

Someone can tell me why?

2/21/2013 3:28:50 PM

Accepted Answer

add tab to load multiple page

The willow browser is a tabbed JavaFX browser sample project - review that to see how this might be done.

add a "Stop" Button to stop loading page,

You could try executing webView.getEngine().getLoadWorker().cancel(); in the action handler for a button. I'm not sure if the WebEngine will respect the cancel request or not.

install Adobe Flash Player to my browser

You cannot - WebView does not support plugins such as Flash.

access Youtube

Use YouTube's html5 compliant video playback interface rather than it's flash based interface.

I created some sample code to play YouTube Videos in a WebView.

WARNING: bad driver version detected, device disabled. Please update your driver to at least version

Your display driver is out of date and doesn't meet the minimum requirements for using JavaFX - you should update it to a newer version.

2/21/2013 10:46:17 PM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow