Example usage for javafx.scene.web WebEvent getData

List of usage examples for javafx.scene.web WebEvent getData

Introduction

In this page you can find the example usage for javafx.scene.web WebEvent getData.

Prototype

public T getData() 

Source Link

Document

Returns data item carried by this event.

Usage

From source file:Main.java

@Override
public void start(final Stage stage) {
    stage.setWidth(400);// ww  w . ja  v  a  2s .  c o m
    stage.setHeight(500);
    Scene scene = new Scene(new Group());

    final WebView browser = new WebView();
    final WebEngine webEngine = browser.getEngine();

    ScrollPane scrollPane = new ScrollPane();
    scrollPane.setContent(browser);

    browser.getEngine().setOnAlert((WebEvent<String> wEvent) -> {
        System.out.println("Alert Event  -  Message:  " + wEvent.getData());
    });

    webEngine.load("http://java2s.com");

    scene.setRoot(scrollPane);

    stage.setScene(scene);
    stage.show();
}

From source file:Main.java

@Override
public void start(final Stage stage) {
    stage.setWidth(400);//from  www  .  jav a 2 s  . com
    stage.setHeight(500);
    Scene scene = new Scene(new Group());

    final WebView browser = new WebView();
    final WebEngine webEngine = browser.getEngine();

    ScrollPane scrollPane = new ScrollPane();
    scrollPane.setContent(browser);

    browser.getEngine().setOnAlert((WebEvent<String> wEvent) -> {
        System.out.println("Alert Event  -  Message:  " + wEvent.getData());
    });

    webEngine.load("http://java2s.com");

    final WebHistory history = webEngine.getHistory();
    history.getEntries().addListener(new ListChangeListener<WebHistory.Entry>() {
        @Override
        public void onChanged(Change<? extends Entry> c) {
            c.next();
            for (Entry e : c.getRemoved()) {
                System.out.println(e.getUrl());
            }
            for (Entry e : c.getAddedSubList()) {
                System.out.println(e.getUrl());
            }
        }
    });

    history.go(0);
    scene.setRoot(scrollPane);

    stage.setScene(scene);
    stage.show();
}

From source file:de.chaosfisch.uploader.gui.renderer.TagTextArea.java

private void initPicker(final WebView webView) {
    webView.getEngine().loadContent(getInlineHtml());
    webView.setContextMenuEnabled(false);
    webView.getEngine().setOnAlert(new EventHandler<WebEvent<String>>() {
        @Override/*ww w .ja  va  2  s .com*/
        public void handle(final WebEvent<String> event) {
            tags.set(event.getData());
        }
    });

    getChildren().add(webView);
}

From source file:gisintegration.SimpleSwingBrowser.java

private void createScene() {
    Platform.runLater(new Runnable() {
        @Override/*  ww w .ja  va 2 s  .  c  o  m*/
        public void run() {

            WebView view = new WebView();
            engine = view.getEngine();

            //                engine.titleProperty().addListener(new ChangeListener<String>() {
            //                    @Override
            //                    public void changed(ObservableValue<? extends String> observable, String oldValue, final String newValue) {
            //                        SwingUtilities.invokeLater(new Runnable() {
            //                            @Override
            //                            public void run() {
            //                                SimpleSwingBrowser.this.setTitle(newValue);
            //                            }
            //                        });
            //                    }
            //                });

            engine.setOnStatusChanged(new EventHandler<WebEvent<String>>() {
                @Override
                public void handle(final WebEvent<String> event) {
                    SwingUtilities.invokeLater(new Runnable() {
                        @Override
                        public void run() {
                            lblStatus.setText(event.getData());
                        }
                    });
                }
            });

            //                engine.locationProperty().addListener(new ChangeListener<String>() {
            //                    @Override
            //                    public void changed(ObservableValue<? extends String> ov, String oldValue, final String newValue) {
            //                        SwingUtilities.invokeLater(new Runnable() {
            //                            @Override
            //                            public void run() {
            //                                txtURL.setText(newValue);
            //                            }
            //                        });
            //                    }
            //                });

            engine.getLoadWorker().workDoneProperty().addListener(new ChangeListener<Number>() {
                @Override
                public void changed(ObservableValue<? extends Number> observableValue, Number oldValue,
                        final Number newValue) {
                    SwingUtilities.invokeLater(new Runnable() {
                        @Override
                        public void run() {
                            progressBar.setValue(newValue.intValue());
                        }
                    });
                }
            });

            engine.getLoadWorker().exceptionProperty().addListener(new ChangeListener<Throwable>() {

                public void changed(ObservableValue<? extends Throwable> o, Throwable old,
                        final Throwable value) {
                    if (engine.getLoadWorker().getState() == FAILED) {
                        SwingUtilities.invokeLater(new Runnable() {
                            @Override
                            public void run() {
                                JOptionPane.showMessageDialog(panel,
                                        (value != null) ? engine.getLocation() + "\n" + value.getMessage()
                                                : engine.getLocation() + "\nUnexpected error.",
                                        "Loading error...", JOptionPane.ERROR_MESSAGE);
                            }
                        });
                    }
                }
            });

            jfxPanel.setScene(new Scene(view));
        }
    });
}

From source file:org.wandora.application.gui.topicpanels.webview.WebViewPanel.java

private void initFX(final JFXPanel fxPanel) {
    Group group = new Group();
    Scene scene = new Scene(group);
    fxPanel.setScene(scene);//from ww w.  j  av a 2 s. co m

    webView = new WebView();

    if (javaFXVersionInt >= 8) {
        webView.setScaleX(1.0);
        webView.setScaleY(1.0);
        //webView.setFitToHeight(false);
        //webView.setFitToWidth(false);
        //webView.setZoom(javafx.stage.Screen.getPrimary().getDpi() / 96);
    }

    group.getChildren().add(webView);

    int w = this.getWidth();
    int h = this.getHeight() - 34;

    webView.setMinSize(w, h);
    webView.setMaxSize(w, h);
    webView.setPrefSize(w, h);

    // Obtain the webEngine to navigate
    webEngine = webView.getEngine();

    webEngine.locationProperty().addListener(new ChangeListener<String>() {
        @Override
        public void changed(ObservableValue<? extends String> observable, String oldValue,
                final String newValue) {
            if (newValue.endsWith(".pdf")) {
                try {
                    int a = WandoraOptionPane.showConfirmDialog(Wandora.getWandora(),
                            "Open PDF document in external application?",
                            "Open PDF document in external application?", WandoraOptionPane.YES_NO_OPTION);
                    if (a == WandoraOptionPane.YES_OPTION) {
                        Desktop dt = Desktop.getDesktop();
                        dt.browse(new URI(newValue));
                    }
                } catch (Exception e) {
                }
            } else {
                SwingUtilities.invokeLater(new Runnable() {
                    @Override
                    public void run() {
                        urlTextField.setText(newValue);
                    }
                });
            }
        }
    });
    webEngine.titleProperty().addListener(new ChangeListener<String>() {
        @Override
        public void changed(ObservableValue<? extends String> observable, String oldValue,
                final String newValue) {
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    title = newValue;
                }
            });
        }
    });
    webEngine.setOnAlert(new EventHandler<WebEvent<java.lang.String>>() {
        @Override
        public void handle(WebEvent<String> t) {
            if (t != null) {
                String str = t.getData();
                if (str != null && str.length() > 0) {
                    WandoraOptionPane.showMessageDialog(Wandora.getWandora(), str, "Javascript Alert",
                            WandoraOptionPane.PLAIN_MESSAGE);
                }
            }
        }
    });
    webEngine.setConfirmHandler(new Callback<String, Boolean>() {
        @Override
        public Boolean call(String msg) {
            int a = WandoraOptionPane.showConfirmDialog(Wandora.getWandora(), msg, "Javascript Alert",
                    WandoraOptionPane.YES_NO_OPTION);
            return (a == WandoraOptionPane.YES_OPTION);
        }
    });
    webEngine.setPromptHandler(new Callback<PromptData, String>() {
        @Override
        public String call(PromptData data) {
            String a = WandoraOptionPane.showInputDialog(Wandora.getWandora(), data.getMessage(),
                    data.getDefaultValue(), "Javascript Alert", WandoraOptionPane.QUESTION_MESSAGE);
            return a;
        }
    });

    webEngine.setCreatePopupHandler(new Callback<PopupFeatures, WebEngine>() {
        @Override
        public WebEngine call(PopupFeatures features) {
            if (informPopupBlocking) {
                WandoraOptionPane.showMessageDialog(Wandora.getWandora(),
                        "A javascript popup has been blocked. Wandora doesn't allow javascript popups in Webview topic panel.",
                        "Javascript popup blocked", WandoraOptionPane.PLAIN_MESSAGE);
            }
            informPopupBlocking = false;
            return null;
        }
    });
    webEngine.setOnVisibilityChanged(new EventHandler<WebEvent<Boolean>>() {
        @Override
        public void handle(WebEvent<Boolean> t) {
            if (t != null) {
                Boolean b = t.getData();
                if (informVisibilityChanges) {
                    WandoraOptionPane.showMessageDialog(Wandora.getWandora(),
                            "A browser window visibility change has been blocked. Wandora doesn't allow visibility changes of windows in Webview topic panel.",
                            "Javascript visibility chnage blocked", WandoraOptionPane.PLAIN_MESSAGE);
                    informVisibilityChanges = false;
                }
            }
        }
    });
    webEngine.getLoadWorker().stateProperty().addListener(new ChangeListener<State>() {
        @Override
        public void changed(ObservableValue ov, State oldState, State newState) {
            if (newState == Worker.State.SCHEDULED) {
                //System.out.println("Scheduled!");
                startLoadingAnimation();
            }
            if (newState == Worker.State.SUCCEEDED) {
                Document doc = webEngine.getDocument();
                try {
                    Transformer transformer = TransformerFactory.newInstance().newTransformer();
                    //transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
                    transformer.setOutputProperty(OutputKeys.METHOD, "xml");
                    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
                    transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
                    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");

                    // transformer.transform(new DOMSource(doc), new StreamResult(new OutputStreamWriter(System.out, "UTF-8")));

                    StringWriter stringWriter = new StringWriter();
                    transformer.transform(new DOMSource(doc), new StreamResult(stringWriter));
                    webSource = stringWriter.toString();
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
                stopLoadingAnimation();
            } else if (newState == Worker.State.CANCELLED) {
                //System.out.println("Cancelled!");
                stopLoadingAnimation();
            } else if (newState == Worker.State.FAILED) {
                webEngine.loadContent(failedToOpenMessage);
                stopLoadingAnimation();
            }
        }
    });

}