在Java中使用Windows资源管理器的“打开文件”功能

3

在Java中使用此代码将打开Windows资源管理器并指向C盘:

Desktop.getDesktop().open(new File("c:\\"));

然而,我也需要突出显示此处的“打开文件”功能:http://i.imgur.com/XfgnozF.jpg

有没有一种方法可以在Java中实现这个(使用Windows Explorer,而不是Swing的FileChooser)?


4
这个图片是来自于 MS Word 的打开对话框,而非 Windows 资源管理器。后者没有“打开”对话框。 - Reimeus
是的,只是作为一个例子。 - Jasmine Mercier
1
你需要更加具体。Windows资源管理器没有打开对话框,而所有的MS Office应用程序都有一个打开对话框。 - Reimeus
1
@JasmineMercier 问题是为什么? JFileChooser 有什么问题吗?你试过使用 FileDialog 吗? - MadProgrammer
1
然后将L&F设置为当前平台的L&F。 - Reimeus
显示剩余4条评论
2个回答

10

看一下在使用本地系统的外观和感觉时如何使用JFileChooser

enter image description here

public class NativeOpenDialogDemo {

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                   ex.printStackTrace();
                }

                final JFrame frame = new JFrame("Open File Example");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                JButton openButton = new JButton("Open");
                openButton.addActionListener(new ActionListener() {
                    
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        JFileChooser chooser = new JFileChooser();
                        if (chooser.showOpenDialog(frame) == JFileChooser.APPROVE_OPTION) {
                            // do something
                        }
                    }
                });
                frame.add(openButton);
                frame.pack();
                frame.setLocationByPlatform(true);
                frame.setVisible(true);
            }
        });
    }
}

1
+1 是为了找到问题和解决方案。 - MadProgrammer

2
我们可以使用JFileChooser。
JFileChooser chooser = new JFileChooser();
            int status = chooser.showOpenDialog(null);
            if (status == JFileChooser.APPROVE_OPTION) {
                File file = chooser.getSelectedFile();
                if (file == null) {
                    return;
                }

                String fileName = chooser.getSelectedFile().getAbsolutePath();
   ......

            }

(使用Windows资源管理器,而不是Swing的FileChooser) - Jasmine Mercier

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接