Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ctnels/208fa2284365bf479be9772d45bb5d23 to your computer and use it in GitHub Desktop.
Save ctnels/208fa2284365bf479be9772d45bb5d23 to your computer and use it in GitHub Desktop.
Demonstrates adding a basic alert handler to a JavaFX WebView.
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();
popup.initOwner(primaryStage);
popup.initStyle(StageStyle.UTILITY);
popup.initModality(Modality.WINDOW_MODAL);
StackPane content = new StackPane();
content.getChildren().setAll(
new Label(event.getData())
);
content.setPrefSize(200, 100);
popup.setScene(new Scene(content));
popup.showAndWait();
}
});
final Scene scene = new Scene(webView);
primaryStage.setScene(scene);
primaryStage.show();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment