New version is available You are viewing the documentation for JxBrowser 6 which is not supported since December 2019. Go to the current documentation.
List icon Contents

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

JavaScript Dialogs

By default JxBrowser doesn’t display JavaScript dialogs and use silent mode where all dialogs are automatically closed emulating behavior when user clicks the Cancel button on the dialog.

JavaScript engine provides three types of dialogs: alert, confirm, and prompt. When JavaScript engine needs to display one of these dialogs, an appropriate method of the DialogHandler is invoked.

The DialogHandler.onAlert() method is invoked when JavaScript alert dialog should be displayed. It happens when the window.alert() JavaScript function is invoked. The dialog displays text message and OK" button. JavaScript execution will be blocked until Alert dialog is closed.

The DialogHandler.onConfirmation() method is invoked when JavaScript confirmation dialog should be displayed. It happens when the window.confirm() JavaScript function is invoked. The dialog displays text message, “OK” and “Cancel” buttons. If the user clicks “OK”, the box returns true. If the user clicks “Cancel”, the box returns false. JavaScript execution will be blocked until Confirm dialog is closed.

The DialogHandler.onPrompt() method is invoked when JavaScript prompt dialog should be displayed. It happens when the window.prompt() JavaScript function is invoked. The dialog displays a text input field where user can enter some text, “OK” and “Cancel” buttons. To provide the new prompt text back to JavaScript you can use the PromptDialogParams.setPromptText(String) method. If the user clicks “OK” the box returns the input value. If the user clicks “Cancel” the box returns null. JavaScript execution will be blocked until Prompt dialog is closed.

To display JavaScript dialogs you can register your own implementation of the DialogHandler which displays appropriate UI dialogs. For example, the following code demonstrates how to register custom DialogHandler implementation that displays Alert dialog using Java Swing API and cancels Confirm and Prompt dialogs:

browser.setDialogHandler(new DialogHandler() {
    @Override
    public void onAlert(DialogParams params) {
        String url = params.getURL();
        String title = "The page at " + url + " says:";
        String message = params.getMessage();
        JOptionPane.showMessageDialog(null, message, title, JOptionPane.PLAIN_MESSAGE);
    }

    @Override
    public CloseStatus onConfirmation(DialogParams params) {
        return CloseStatus.CANCEL;
    }

    @Override
    public CloseStatus onPrompt(PromptDialogParams params) {
        return CloseStatus.CANCEL;
    }
    ...
});

Or you can register JxBrowser’s DialogHandler implementation that already implements and displays UI dialogs:

Swing

import com.teamdev.jxbrowser.chromium.Browser;
import com.teamdev.jxbrowser.chromium.swing.BrowserView;
import com.teamdev.jxbrowser.chromium.swing.DefaultDialogHandler;

Browser browser = new Browser();
BrowserView browserView = new BrowserView(browser);
browser.setDialogHandler(new DefaultDialogHandler(browserView));

JavaFX

import com.teamdev.jxbrowser.chromium.Browser;
import com.teamdev.jxbrowser.chromium.javafx.BrowserView;
import com.teamdev.jxbrowser.chromium.javafx.DefaultDialogHandler;

Browser browser = new Browser();
BrowserView browserView = new BrowserView(browser);
browser.setDialogHandler(new DefaultDialogHandler(browserView));

Open File

Using JxBrowser DialogHandler API you can handle situation when web page needs to display the File Open dialog (e.g. when user clicks the file input element on a web form). Using this API you can display your own File Open dialog or suppress the dialog at all and provide path to a file program.

By default JxBrowser displays standard Java Swing/JavaFX File Open dialog.

The following sample demonstrates how to display your File Open dialog using Java Swing API:

browser.setDialogHandler(new DefaultDialogHandler(view) {
    @Override
    public CloseStatus onFileChooser(final FileChooserParams params) {
        final AtomicReference<CloseStatus> result = new AtomicReference<CloseStatus>(
                CloseStatus.CANCEL);

        try {
            SwingUtilities.invokeAndWait(new Runnable() {
                @Override
                public void run() {
                    if (params.getMode() == FileChooserMode.Open) {
                        JFileChooser fileChooser = new JFileChooser();
                        if (fileChooser.showOpenDialog(view)
                                == JFileChooser.APPROVE_OPTION) {
                            File selectedFile = fileChooser.getSelectedFile();
                            params.setSelectedFiles(selectedFile.getAbsolutePath());
                            result.set(CloseStatus.OK);
                        }
                    }
                }
            });
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }

        return result.get();
    }
});

Before Unload

The onbeforeunload event fires when the document is about to be unloaded. This event allows you to display a message in a confirmation dialog box to inform the user whether he/she wants to stay or leave the current page. JxBrowser API allows handling this dialog using DialogHandler API. By default the dialog is displayed. Using your custom implementation of the DialogHandler, you can handle this dialog in your own way. For example, you can display your custom message dialog or suppress the dialog and don’t allow unloading web page.

The following sample demonstrates how to handle the onbeforeunload dialog:

browser.setDialogHandler(new DefaultDialogHandler(view) {
    @Override
    public CloseStatus onBeforeUnload(UnloadDialogParams params) {
        String title = "Confirm Navigation";
        String message = params.getMessage();
        int returnValue = JOptionPane.showConfirmDialog(view, message, title, JOptionPane.OK_CANCEL_OPTION);
        if (returnValue == JOptionPane.OK_OPTION) {
            return CloseStatus.OK;
        } else {
            return CloseStatus.CANCEL;
        }
    }
});

Select Color

JxBrowser supports HTML Input element with type=color and displays default color chooser dialog where user can select required color. You can override default behavior by registering your own DialogHandler implementation with overridden onColorChooser() method where you can set the required color programmatically without displaying any UI dialogs. The following code demonstrates how to do this:

browser.setDialogHandler(new DefaultDialogHandler(view) {
    @Override
    public CloseStatus onColorChooser(ColorChooserParams params) {
        params.setColor(Color.BLUE);
        return CloseStatus.OK;
    }
});