2 Replies Latest reply: Jun 7, 2013 9:00 AM by dev RSS

    javafx and javascript problem

      Hi there,

      I'm working with Javafx and facing a problem regarding javafx and javascript event. I'm using WebView of javafx and embedding it with swing application.
      I'm passing an url to it, and able to see the webpage.

      The webpage contain a button which has a validation. After clicking upon, A popup alert is supposed to be opened against that validation.

      But the alert is not coming.
      could you please enlight on this.

        • 1. Re: javafx and javascript problem
          Note that your question belongs in the JavaFX 2.0 and later forum not the JavaFX 1.x forum (JavaFX 1.x did not have a WebView).

          JavaFX 2.0 and Later "JavaFX 2.0 and later forum"


          To get popups for WebView, you have to implement an appropriate handler.
          Maybe a future version of a WebView will provide default implementations of these dialogs, but there is currently no default behavior for most of these methods in JavaFX 2.2 (i.e. by and large they are just ignored).

          See WebEngine methods:

          For your example you probably want to provide an EventHandler for setOnAlert which creates a new dialog containing the alert information.

          For example the following code will display a couple of alerts when the user triggers them by pressing an html button with an onclick javascript handler:
          import javafx.application.Application;
          import static javafx.application.Application.launch;
          import javafx.event.EventHandler;
          import javafx.scene.Scene;
          import javafx.scene.control.Label;
          import javafx.scene.layout.StackPane;
          import javafx.scene.web.*;
          import javafx.stage.*;
          public class WebViewWithPromptHandler extends Application {
            public static void main(String[] args) { launch(args); }
            @Override public void start(final Stage primaryStage) {
              WebView webView = new WebView();
              webView.getEngine().loadContent("<button onclick='alert(\"Alerted\"); alert(\"Alerted 2\");'>Popup</button>");
              webView.getEngine().setOnAlert(new EventHandler<WebEvent<String>>() {
                @Override public void handle(WebEvent<String> event) {
                  Stage popup = new Stage();
                  StackPane content = new StackPane();
                    new Label(event.getData())
                  content.setPrefSize(200, 100);
                  popup.setScene(new Scene(content));
              final Scene scene = new Scene(webView);

          If you want to make use of a predefined dialog logic and template for defining your WebView dialog handlers, then refer to the ControlsFX project:
          • 2. Re: javafx and javascript problem
            Hi jsmith,

            Sorry I was offline for a long days. Thanks for your reply. It is done.Thanks again.

            I have another one issue.

            I have a download option in the web page on which, when I click, the browser's default download window is opened and a report is downloaded.

            It is integrated in Javafx WebView.

            The download option is showing.

            when I click on that, nothing is happening whereas the same functionality is working in the browser.

            Could you please help me out.