且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在不使用JFileChooser的情况下打开Java Swing中的文件

更新时间:2023-01-15 09:46:41

JFileChooser with the native PLAF seems to fulfill the stated requirement.

import javax.swing.JFileChooser;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

public class NativeFileChooser {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(
                            UIManager.getSystemLookAndFeelClassName());
                } catch(Exception e) {
                    e.printStackTrace();
                }
                JFileChooser jfc = new JFileChooser();
                jfc.showOpenDialog(null);
            }
        });
    }
}

Still not quite to your liking? Then you might start with this one & change it to need:


..so I guess SWT and Swing don't mix together?

It is generally not a good idea to mix Swing/AWT/SWT components in the same top-level container. It is not a problem to open an AWT FileDialog over a Swing based JFrame since they are both top-level containers. I am pretty sure the same would apply to Swing/SWT or AWT/SWT.