This guide shows how to handle the requests to display various dialogs.

By default JxBrowser does not display dialogs and is set to the silent mode where all the dialogs are automatically closed as if the user clicked the Cancel button in the dialog.

To change the default behavior you will need to register your own implementation of the appropriate callback for a particular dialog. In your implementation you decide how to display the dialog and provide the results to the engine. You can also create and display every dialog with given parameters.

JavaScript Dialogs

Alert

When a JavaScript alert dialog should be displayed the AlertCallback callback is invoked. It happens when the window.alert() JavaScript function is invoked.

In this callback you can obtain the dialog parameters such as title, message, and localized text of the “OK” action. When the dialog is closed callback.ok() must be called. For example:

browser.set(AlertCallback.class, (params, callback) -> {
    // Dialog title.
    String title = params.getTitle();
    // Dialog message.
    String message = params.getMessage();
    // The localized text of the "OK" callback action.
    String okActionText = params.getOkActionText();
    // Create and display the dialog if necessary.
    ...
    // The dialog has been closed.
    callback.ok();
});

JavaScript execution will be blocked until the Alert dialog is closed.

Confirm

When a JavaScript confirm dialog should be displayed the ConfirmCallback callback is invoked. It happens when the window.confirm() JavaScript function is invoked.

In this callback you can obtain the dialog parameters such as title, message, localized text of the “Yes” and “No” actions. When the dialog is closed callback.yes() or callback.no() must be called. For example:

browser.set(ConfirmCallback.class, (params, callback) -> {
    // Dialog title.
    String title = params.getTitle();
    // Dialog message.
    String message = params.getMessage();
    // The localized text of the "Yes" action.
    String yesActionText = params.getYesActionText();
    // The localized text of the "No" action.
    String noActionText = params.getNoActionText();
    ...
    // The "Yes" action has been selected.
    callback.yes();
});

JavaScript execution will be blocked until the Confirm dialog is closed.

Prompt

When a JavaScript prompt dialog should be displayed the PromptCallback callback is invoked. It happens when the window.prompt() JavaScript function is invoked.

In this callback you can obtain the dialog parameters such as title, message, text, localized text of the “OK” and “Cancel” actions. When the dialog is closed callback.ok(String) or callback.cancel() must be called. For example:

browser.set(PromptCallback.class, (params, callback) -> {
    // Dialog title.
    String title = params.getTitle();
    // Dialog message.
    String message = params.getMessage();
    // The localized text of the "OK" action.
    String okActionText = params.getOkActionText();
    // The localized text of the "Cancel" action.
    String cancelActionText = params.getCancelActionText();
    ...
    // A text has been entered and the "OK" action has been selected.
    callback.ok("Entered text");
});

JavaScript execution will be blocked until the Prompt dialog is closed.

BeforeUnload

The onbeforeunload event is fired when the web page is about to be unloaded. This event allows you to display a message in a confirmation dialog to inform the user whether they want to stay or leave the current page.

When the confirmation dialog should be displayed the BeforeUnloadCallback callback is invoked.

In this callback you can obtain the dialog parameters such as title, message, localized text of the “Stay” and “Leave” actions. When the dialog is closed callback.stay() or callback.leave() must be called. For example:

browser.set(BeforeUnloadCallback.class, (params, callback) -> {
    // Dialog title.
    String title = params.getTitle();
    // Dialog message.
    String message = params.getMessage();
    // The localized text of the "Stay" action.
    String stayActionText = params.getStayActionText();
    // The localized text of the "Leave" action.
    String leaveActionText = params.getLeaveActionText();
    ...
    // The "Stay" action has been selected.
    callback.stay();
});

Select Color

When a color should be selected a SelectColorCallback callback is invoked. It happens when a user clicks the input element with the color type:

<input type="color" value="#ff0000">

In this callback you can obtain the dialog parameters such as default color. When the dialog is closed callback.select(Color) or callback.cancel() must be called. For example:

browser.set(SelectColorCallback.class, (params, callback) -> {
    // The default color.
    Color defaultColor = params.getColor();
    // The selected color.
    Color selectedColor = Color.newBuilder()
            .setRed(1.0f)
            .setGreen(1.0f)
            .setBlue(1.0f)
            .build();
    ...
    // The given color has been selected.
    callback.select(selectedColor);
});

Open File

When a web page wants the user to choose a file from their device storage the OpenFileCallback callback is invoked. It happens when the user clicks the input element with the file type:

<input type="file" accept="image/png, image/jpeg">

In this callback you can obtain the dialog parameters such as default file name, acceptable file extensions, and description of the acceptable file extensions. When the dialog is closed callback.open(File) or callback.cancel()must be called. For example:

browser.set(OpenFileCallback.class, (params, callback) -> {
    // The default file name.
    String defaultFileName = params.getFileName();
    // Acceptable extensions.
    List<String> acceptableExtensions = params.getAcceptableExtensionsList();
    ...
    // The given file should be opened.
    callback.open(new File("<path-to-selected-file>"));
});

Open Files

When a web page wants the user choose multiple files from their device storage the OpenFilesCallback callback is invoked. It happens when the user clicks the input element with the file type and the multiple attribute:

<input type="file" accept="image/png, image/jpeg" multiple>

In this callback you can obtain the dialog parameters such as default file name, acceptable file extensions, and description of the acceptable file extensions. When the dialog is closed callback.open(File...) or callback.cancel() must be called. For example:

browser.set(OpenFilesCallback.class, (params, callback) -> {
    // Acceptable extensions.
    List<String> acceptableExtensions = params.getAcceptableExtensionsList();
    ...
    File file1 = new File("<path-to-selected-file1>");
    File file2 = new File("<path-to-selected-file1>");
    // The given files should be opened.
    callback.open(file1, file2);
});

Open Folder

When Chromium wants the user to choose a folder from their device storage the OpenFolderCallback callback is invoked.

If necessary, you can create and display a dialog where the user can choose the folder. When the dialog is closed callback.open(File) or callback.cancel() must be called. For example:

browser.set(OpenFolderCallback.class, (params, callback) -> {
    ...
    // The given folder should be opened.
    callback.open(new File("<path-to-folder>"));
});

Save File

When Chromium wants the user to choose a file where some data should be saved the SaveFileCallback callback is invoked. It might happen in different cases:

  • when Chromium cannot display a media file because its format is unsupported it suggests to save the file in a local file system.
  • when saving a web page as PDF document through the Print Preview dialog.
  • when clicking the Save button in PDF Viewer.

In this callback you can obtain the dialog parameters such as default file name. When the dialog is closed callback.save(File) or callback.cancel() must be called. For example:

browser.set(SaveFileCallback.class, (params, callback) -> {
    // The default file name.
    String defaultFileName = params.getFileName();
    ...
    // The given file should be saved.
    callback.save(new File("<path-to-file>"));
});

Select Client Certificate

When Chromium wants the user to select a client SSL certificate from the list of available certificates the SelectClientCertificateCallback callback is invoked.

In this callback you can obtain the dialog parameters such as dialog title, message, localized text of the “Select” and “Cancel” actions, the list of available certificates. When the dialog is closed callback.select(int) or callback.cancel() must be called. For example:

browser.set(SelectClientCertificateCallback.class, (params, callback) -> {
    // The dialog title.
    String title = params.getTitle();
    // The dialog message.
    String message = params.getMessage();
    // The localized text of the "Select" action.
    String selectActionText = params.getSelectActionText();
    // The localized text of the "Cancel" action.
    String cancelActionText = params.getCancelActionText();
    // Available SSL certificates.
    List<Certificate> certificates = params.getCertificatesList();
    // The index of the last certificate in the list.
    int certificateIndex = certificates.size() - 1;
    ...
    // The last certificate in the list has been selected.
    callback.select(certificateIndex);
});
Go Top