/** * Copyright (c) 2008, 2011 Oracle and/or its affiliates. * All rights reserved. Use is subject to license terms. */ package webviewbrowser; 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.VPos; import javafx.scene.Group; 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.Priority; import javafx.scene.web.WebEngine; import javafx.scene.web.WebView; import javafx.stage.Stage; /** * Demonstrates a WebView object accessing a web page. * * @see javafx.scene.web.WebView * @see javafx.scene.web.WebEngine */ public class WebViewBrowser extends Application { private void init(Stage primaryStage) { Group root = new Group(); primaryStage.setScene(new Scene(root)); WebView view = new WebView(); view.setMinSize(1024, 768); view.setPrefSize(1024, 768); final WebEngine eng = view.getEngine(); eng.load("http://www.oracle.com/us/index.html"); final TextField locationField = new TextField("http://www.oracle.com/us/index.html"); locationField.setMaxHeight(Double.MAX_VALUE); Button goButton = new Button("Go"); goButton.setDefaultButton(true); EventHandler goAction = new EventHandler() { @Override public void handle(ActionEvent event) { eng.load(locationField.getText()); } }; goButton.setOnAction(goAction); locationField.setOnAction(goAction); eng.locationProperty().addListener(new ChangeListener() { @Override public void changed(ObservableValue observable, String oldValue, String newValue) { locationField.setText(newValue); } }); GridPane grid = new GridPane(); grid.setVgap(5); grid.setHgap(5); GridPane.setConstraints(locationField, 0, 0, 1, 1, HPos.CENTER, VPos.CENTER, Priority.ALWAYS, Priority.SOMETIMES); GridPane.setConstraints(goButton,1,0); GridPane.setConstraints(view, 0, 1, 2, 1, HPos.LEFT, VPos.CENTER, Priority.ALWAYS, Priority.ALWAYS); grid.getColumnConstraints().addAll( new ColumnConstraints(500, 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); root.getChildren().add(grid); } @Override public void start(Stage primaryStage) throws Exception { init(primaryStage); primaryStage.show(); } public static void main(String[] args) { launch(args); } }