Issue
I am trying to open webpage in webview using JavaFx . Its opening the web page properly but its not supporting the Ajax based web features like partial refreshing and new window popup handling I am using the following code
final Group group= new Group();
Scene scene= new Scene(group);
fxpanel.setScene(scene);
WebView webview = new WebView ();
group.getChildren().add(webview);
eng= webview.getEngine();
eng.setJavaScriptEnabled(true);
try{
String url="http://www.abc.com";
eng.load(url);
eng.setCreatePopupHandler(
new Callback<PopupFeatures, WebEngine>() {
@Override
public WebEngine call(PopupFeatures config) {
smallView = new WebView();
smallView.setFontScale(0.8);
ChatPopup frm = new ChatPopup(smallView);
frm.setBounds(0,0,400,250);
frm.setVisible(true);
return smallView.getEngine();
}
});
}
catch(Exception ex){}
}
Solution
WebView does support Ajax.
- Run the following app.
- Click on the "Load data from server into div" button.
- Page will be refreshed with data fetched from the server.
import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.scene.Scene;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
public class WebViewAjax extends Application {
public static void main(String[] args) { launch(args); }
@Override public void start(Stage stage) {
WebView webView = new WebView();
webView.getEngine().load("http://www.jquerysample.com/#BasicAJAX");
final Scene scene = new Scene(webView);
stage.setScene(scene);
stage.show();
}
}
Aside on alerts
Note, in the sample page linked above there there are numerous examples for handling the json data. Some of the examples, e.g. the jquery $.get()
example, output the result of the ajax call using a JavaScript alert()
.
If you want to see the alert data, you need to add an alert handler to the WebView engine. A basic alert handler such as below will just output the alert data to the output console:
webView.getEngine().setOnAlert(
stringWebEvent -> System.out.println(stringWebEvent.getData())
);
This is not really related to ajax calls, but without an alert handler, you may be confused you if you are trying to use an alert to debug or show data returned from a WebView ajax call.
Answered By - jewelsea
Answer Checked By - Mildred Charles (JavaFixing Admin)