通过JNA调用GetOpenFileName对于Swing应用程序失败 [英] Calling GetOpenFileName through JNA fails for Swing app

查看:153
本文介绍了通过JNA调用GetOpenFileName对于Swing应用程序失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Java中的本机Windows文件对话框,使用JNA调用comdlg32函数 GetOpenFileName 。我做了一个静态方法, OpenFileDialog.display 看起来像这样:

I'm trying to use the native Windows file dialog in Java, using JNA to call the comdlg32 function GetOpenFileName. I've made a static method, OpenFileDialog.display that looks like this:

 public static List<File> display(Window parent, boolean allowMultiSelect)

它应返回所选文件,如果用户取消则返回null对话框。

It should return the selected files, or null if the user canceled the dialog.

我有两个简单的测试程序。这个按预期工作:

I have two simple test programs. This one works as expected:

package nativedialogs;

import java.io.File;
import java.util.List;

public class SimpleTest {

    public static void main(String[] args) {
        List<File> files = OpenFileDialog.display(null, true);
        System.out.println(files);
    }
}

但是,这个不是:

package nativedialogs;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class SwingTest {

    public static void main(String[] args) {

        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                JFrame f = new JFrame();
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                JButton button = new JButton("Open file dialog");
                button.addActionListener(new ActionListener() {

                    @Override
                    public void actionPerformed(ActionEvent e) {
                        List<File> files = OpenFileDialog.display(f, true);
                        // These also fail:
                        // List<File> files = OpenFileDialog.display(f, false);
                        // List<File> files = OpenFileDialog.display(null, true);
                        // List<File> files = OpenFileDialog.display(null, false);
                        System.out.println(files);
                    }
                });
                f.add(button);
                f.pack();
                f.setVisible(true);
            }
        });
    }
}

对于后一个例子, CommDlgExtendedError 返回2,其中的CommDlgExtendedError的MSDN页面>:

For the latter example, CommDlgExtendedError returns 2, which according to MSDN is:


CDERR_INITIALIZATION 0x0002

CDERR_INITIALIZATION 0x0002

常规对话框功能在初始化期间失败。当没有足够的内存时,通常会发生此错误。

The common dialog box function failed during initialization. This error often occurs when sufficient memory is not available.

...这对我来说并没有多大帮助。我在这里做错了什么?

...which doesn't really help me all that much. What am I doing wrong here?

我把其他来源放在PasteBin上所以我不会弄乱这个问题太多:

I've put the other sources on PasteBin so I wouldn't clutter the question too much:

OpenFileDialog http://pastebin.com/HDmu0TjX

ComDlg32JNA http://pastebin.com/X5F5LLip

推荐答案

最好不要从Swing EDT中执行任何JNA代码。尝试使用 SwingWorker 来运行对话框后台线程。

It's better not to do any JNA code from the Swing EDT. Try using SwingWorker to run the dialog in a background thread.

我会尝试提供更多帮助,但我的Win 7 64位上没有comdlg32 :(

I'd try to help more, but there is no comdlg32 on my Win 7 64-bit :(

这篇关于通过JNA调用GetOpenFileName对于Swing应用程序失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆