2

i am pretty much new to javafx. i have built an application on javafx which will load index.html page through WebView. when i click on register button in index.html new registration form page will be loaded.

javafx code:

public void start(final Stage primaryStage) throws Exception {
    primaryStage.setResizable(false);
    FrontEndFinal f=new FrontEndFinal();

    final WebView browser = new WebView();
    final WebEngine webEngine = browser.getEngine();
    webEngine.setJavaScriptEnabled(true);
    Worker<Void> worker = webEngine.getLoadWorker();
    worker.stateProperty().addListener(new ChangeListener<State>() {

        public void changed(ObservableValue<? extends State> observable, //
                State oldValue, State newValue) {

            // When load successed.
            if (newValue == Worker.State.SUCCEEDED) {

                JSObject jsobj = (JSObject) webEngine.executeScript("window");

                jsobj.setMember("myJavaMember", new FrontEndFinal());
            }
        }
    });
    VBox root = new VBox();

    root.setMinHeight(500);
    root.setMinWidth(1250);


    System.out.println(System.getProperty("user.dir"));
    String p=System.getProperty("user.dir");
    String [] t=p.split("\\\\");
    System.out.println(t.length);
    String final1=t[0]+"/";
    for(int i=1;i<t.length;i++)
    {
        final1=final1+t[i]+"/";
    }
    System.out.println(final1);


    browser.getEngine().load("file:///"+final1+"/Html"+"/SMS.html");
    root.getChildren().add(browser); 
}

When I run this code it loads index.html

in Index.html:

<button class="dropbtn"  onclick="location.href = 'Registration.html'"><b>< Register ></b></button>

after clicking button it opens Registration.html

In Registration.html

<form name="myForm"  method="post">
Name: <input type="text" name="fname">
<button type="button" name="Submit" onclick="abc()">Submit</button>
<script>
function abc(){
    window.alert("Invalid name");
}

Now my problem is when i submit Registration.html form with name field blank, it does not shows alert box i googled many sites like:[https://community.oracle.com/thread/2540279] but didn't work Any help or suggestions?

Thanks In Advance

0

1 Answer 1

2

You need to set an onAlert handler on the web engine. This gives you the ability to configure how you want your application to respond to Javascript alert() callbacks. To show a simple dialog message:

webEngine.setOnAlert(event -> {
    Alert alert = new Alert(Alert.AlertType.INFORMATION);
    alert.setContentText(event.getData());
    alert.showAndWait();
});

Here is a SSCCE:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;

public class WebViewAlertTest extends Application {

    private final String HTML = ""
            + "<html>"
            + "<body>"
            + "<button onclick='alert(\"Javascript Alert\")'>Submit</button>"
            + "</body>"
            + "</html>";

    @Override
    public void start(Stage primaryStage) {
        WebView webView = new WebView();
        WebEngine webEngine = webView.getEngine();
        webEngine.setOnAlert(event -> {
            Alert alert = new Alert(Alert.AlertType.INFORMATION);
            alert.setHeaderText("Message from the web page");
            alert.setContentText(event.getData());
            alert.showAndWait();
        });
        webEngine.loadContent(HTML);

        Scene scene = new Scene(webView, 400, 400);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Hi @James_D Thanks for you reply...but when i run above code it shows error saying event cannot be resolved to a variable.
@sagnemsagnem The code works fine. I added a complete example that you can run.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.