Note: Advice in this article will only work for JxBrowser 6. See the corresponding article for JxBrowser 7 here.(注意:本文中的建议仅适用于JxBrowser6,JxBrowser7相应文章请点击这里。)

Using JxBrowser DialogHandler API you can handle situation when web page needs to display File Open dialog (e.g. when user clicks 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.(使用JxBrowser DialogHandler API,您可以处理网页需要显示“文件打开”对话框时的情况(例如,当用户单击Web表单上的“文件输入”元素时)。使用此API,您可以显示自己的“文件打开”对话框或根本不显示该对话框,并提供文件程序的路径。)

Note: By default JxBrowser displays standard Java Swing/JavaFX File Open dialog.(注意:默认情况下,JxBrowser显示标准Java Swing / JavaFX File Open对话框。)

The following sample demonstrates how to display your File Open dialog using Java Swing API: (以下示例演示了如何使用Java Swing API显示“文件打开”对话框:)

import com.teamdev.jxbrowser.chromium.Browser;
import com.teamdev.jxbrowser.chromium.CloseStatus;
import com.teamdev.jxbrowser.chromium.FileChooserMode;
import com.teamdev.jxbrowser.chromium.FileChooserParams;
import com.teamdev.jxbrowser.chromium.swing.BrowserView;
import com.teamdev.jxbrowser.chromium.swing.DefaultDialogHandler;
import java.awt.BorderLayout;
import java.lang.reflect.InvocationTargetException;
import java.util.concurrent.atomic.AtomicReference;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;

 * The sample demonstrates how to register your DialogHandler and
 * override the functionality that displays file chooser when
 * user uploads file using INPUT TYPE="file" HTML element on a web page.
public class FileUploadSample {
    public static void main(String[] args) {
        Browser browser = new Browser();
        final BrowserView view = new BrowserView(browser);

        JFrame frame = new JFrame();
        frame.add(view, BorderLayout.CENTER);
        frame.setSize(700, 500);

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

                try {
                    SwingUtilities.invokeAndWait(new Runnable() {
                        public void run() {
                            if (params.getMode() == FileChooserMode.Open) {
                                JFileChooser fileChooser = new JFileChooser();
                                if (fileChooser.showOpenDialog(view)
                                        == JFileChooser.APPROVE_OPTION) {
                                    File selectedFile = fileChooser.getSelectedFile();
                } catch (InterruptedException e) {
                } catch (InvocationTargetException e) {

                return result.get();

Click Choose File button to display File Open dialog and select file to upload:(单击选择文件按钮以显示“打开文件”对话框,然后选择要上传的文件:)

Swing File Open dialog