package fr.mmteam; import javafx.application.Application; import javafx.beans.value.ChangeListener; import javafx.beans.value.ObservableValue; import javafx.concurrent.Worker; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.scene.Scene; import javafx.scene.control.ButtonBuilder; import javafx.scene.control.LabelBuilder; import javafx.scene.effect.BoxBlur; import javafx.scene.input.KeyEvent; import javafx.scene.input.MouseEvent; import javafx.scene.layout.HBoxBuilder; import javafx.scene.layout.PaneBuilder; import javafx.scene.layout.Region; import javafx.scene.layout.StackPaneBuilder; import javafx.scene.paint.Color; import javafx.scene.web.WebEngine; import javafx.scene.web.WebEvent; import javafx.scene.web.WebView; import javafx.stage.Modality; import javafx.stage.Stage; import javafx.stage.StageStyle; import org.w3c.dom.html.HTMLElement; public class Main extends Application { private Scene scene; private Stage primaryStage; @Override public void start(Stage stage) throws Exception { this.primaryStage = stage; // create scene stage.setTitle("Web View"); scene = new Scene(new Browser(),750,500, Color.web("#666970")); stage.setScene(scene); // show stage stage.show(); } public static void main(String[] args){ launch(args); } class Browser extends Region { final WebView browser = new WebView(); final WebEngine webEngine = browser.getEngine(); public Browser() { // load the home page webEngine.load("http://google.fr"); browser.autosize(); browser.setOnKeyTyped(new EventHandler() { public void handle(KeyEvent event) { System.out.println(event.getCharacter()); }; }); browser.setOnMousePressed(new EventHandler() { public void handle(MouseEvent event) { Object target = webEngine.executeScript("document.elementFromPoint(" + event.getSceneX() + ", " + event.getSceneY() +")"); if (target instanceof HTMLElement) { System.out.println(((HTMLElement) target).getTagName() + " pressed"); } }; }); browser.setOnMouseClicked(new EventHandler() { public void handle(MouseEvent event) { Object target = webEngine.executeScript("document.elementFromPoint(" + event.getSceneX() + ", " + event.getSceneY() +")"); if (target instanceof HTMLElement) { System.out.println(((HTMLElement) target).getTagName() + " clicked"); } }; }); browser.setOnMouseReleased(new EventHandler() { public void handle(MouseEvent event) { Object target = webEngine.executeScript("document.elementFromPoint(" + event.getSceneX() + ", " + event.getSceneY() +")"); if (target instanceof HTMLElement) { System.out.println(((HTMLElement) target).getTagName() + " released"); } }; }); webEngine.setOnAlert(new EventHandler>() { public void handle(WebEvent event) { alert(browser, event.getData()); }; }); // show the confirmation dialog each time a new page is loaded. webEngine.getLoadWorker().stateProperty().addListener(new ChangeListener() { @Override public void changed(ObservableValue observableValue, Worker.State state, Worker.State newState) { if (newState.equals(Worker.State.SUCCEEDED)) { System.out.println("State change from " + state.toString() + " to " + newState.toString()); System.out.println(observableValue.toString()); System.out.println(webEngine.getLocation()); } } }); getChildren().add(browser); } private void alert(final WebView webView, String msg) { // initialize the confirmation dialog primaryStage.getScene().getRoot().setEffect(new BoxBlur()); final Stage util = new Stage(StageStyle.TRANSPARENT); util.initModality(Modality.APPLICATION_MODAL); util.setScene( new Scene( StackPaneBuilder.create().children( PaneBuilder.create().styleClass("modal-dialog-glass").build(), HBoxBuilder.create().styleClass("modal-dialog-content").children( LabelBuilder.create().text(msg).build(), ButtonBuilder.create().text("OK").defaultButton(true).onAction(new EventHandler() { @Override public void handle(ActionEvent actionEvent) { primaryStage.getScene().getRoot().setEffect(null); util.close(); } }).build() ).build() ).build() , Color.TRANSPARENT ) ); util.getScene().getStylesheets().add(getClass().getResource("modal-dialog.css").toExternalForm()); util.showAndWait(); } } }