My java web browser don't display alert("message"); and confirm("message"); either, I ussually use c# web Browser component and it's works perfectly but I'm new at this.
public void openPage(String url){
JFXPanel jfxPanel = new JFXPanel();
JFrame frame = new JFrame("");
frame.add(jfxPanel);
Platform.runLater(() -> {
WebView webView = new WebView();
jfxPanel.setScene(new Scene(webView));
webView.getEngine().load(url);
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
});
}
For alert
, register an onAlert
handler with the web engine. For confirm
, register a confirmHandler
with the web engine. See the "User interface callbacks" section of the WebEngine
documentation.
Here's a quick example:
import java.util.Optional;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.ButtonType;
import javafx.scene.control.Dialog;
import javafx.scene.layout.BorderPane;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
public class WebViewWithAlertAndConfirm extends Application {
private WebEngine engine;
@Override
public void start(Stage primaryStage) {
WebView webView = new WebView();
engine = webView.getEngine();
engine.setOnAlert(event -> showAlert(event.getData()));
engine.setConfirmHandler(message -> showConfirm(message));
String content =
"<html>"
+ "<head>"
+ "<script language='javascript'>"
+ "function doConfirm() {"
+ " var accepted = confirm('Are you sure?');"
+ " if (accepted) {"
+ " document.getElementById('result').innerHTML = 'Accepted';"
+ " } else {"
+ " document.getElementById('result').innerHTML = 'Declined';"
+ " }"
+ "}"
+ "</script>"
+ "<body>"
+ "<div><button onclick='alert(\"This is an alert!\")'>Show alert</button>"
+ "<button onclick='doConfirm()'>Confirm</button>"
+ "</div>"
+ "<div id='result'/>"
+ "</body>"
+ "</html>";
engine.loadContent(content);
primaryStage.setScene(new Scene(new BorderPane(webView)));
primaryStage.show();
}
private void showAlert(String message) {
Dialog<Void> alert = new Dialog<>();
alert.getDialogPane().setContentText(message);
alert.getDialogPane().getButtonTypes().add(ButtonType.OK);
alert.showAndWait();
}
private boolean showConfirm(String message) {
Dialog<ButtonType> confirm = new Dialog<>();
confirm.getDialogPane().setContentText(message);
confirm.getDialogPane().getButtonTypes().addAll(ButtonType.YES, ButtonType.NO);
boolean result = confirm.showAndWait().filter(ButtonType.YES::equals).isPresent();
// for debugging:
System.out.println(result);
return result ;
}
public static void main(String[] args) {
launch(args);
}
}
I found a pretty solution :)
for alert:
webView.getEngine().setOnAlert(new EventHandler<WebEvent<String>>(){
@Override
public void handle(WebEvent<String> arg0) {
JOptionPane.showMessageDialog(null, arg0.getData(),"Mensaje", JOptionPane.INFORMATION_MESSAGE);
}
});
for confirm:
webView.getEngine().setConfirmHandler(new Callback<String, Boolean>() {
public Boolean call(String msg) {
int result = JOptionPane.showConfirmDialog(null,msg, "Mensaje",JOptionPane.OK_CANCEL_OPTION, JOptionPane.INFORMATION_MESSAGE);
boolean b = (result != 0);
return !b;
}});