Does Swing support Windows 7-style file choosers?

前端 未结 10 1833
星月不相逢
星月不相逢 2020-11-28 08:26

I just added a standard \"Open file\" dialog to a small desktop app I\'m writing, based on the JFileChooser entry of the Swing Tutorial. It\'s generating a

相关标签:
10条回答
  • 2020-11-28 09:15

    I don't believe Swing would cover that though it may, if it doesn't you may need to look at something like SWT, which would make use of the actual native component, or do a custom UI element, like something out of the "Filthy Rich Clients" book.

    0 讨论(0)
  • 2020-11-28 09:16

    good question +1 , looks like as they "forgot" to implements something for Win7 (defaultLookAndFeel) into Java6, but for WinXP works correclty, and I hope too, that there must exists some Method/Properties for that

    anyway you can try that with this code,

    import java.io.File;
    import javax.swing.*;
    import javax.swing.filechooser.FileFilter;
    
    class ChooserFilterTest {
    
        public static void main(String[] args) {
            Runnable r = new Runnable() {
    
                @Override
                public void run() {
                    String[] properties = {"os.name", "java.version", "java.vm.version", "java.vendor"};
                    for (String property : properties) {
                        System.out.println(property + ": " + System.getProperty(property));
                    }
                    JFileChooser jfc = new JFileChooser();
                    jfc.showOpenDialog(null);
                    jfc.addChoosableFileFilter(new FileFilter() {
    
                        @Override
                        public boolean accept(File f) {
                            return f.isDirectory() || f.getName().toLowerCase().endsWith(".obj");
                        }
    
                        @Override
                        public String getDescription() {
                            return "Wavefront OBJ (*.obj)";
                        }
    
                        @Override
                        public String toString() {
                            return getDescription();
                        }
                    });
                    int result = JOptionPane.showConfirmDialog(null, "Description was 'All Files'?");
                    System.out.println("Displayed description (Metal): " + (result == JOptionPane.YES_OPTION));
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                        SwingUtilities.updateComponentTreeUI(jfc);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    jfc.showOpenDialog(null);
                    result = JOptionPane.showConfirmDialog(null, "Description was 'All Files'?");
                    System.out.println("Displayed description (System): " + (result == JOptionPane.YES_OPTION));
                }
            };
            SwingUtilities.invokeLater(r);
        }
    
        private ChooserFilterTest() {
        }
    }
    
    0 讨论(0)
  • 2020-11-28 09:17

    John McCarthy's answer seems to be the best. Here some suggestions.

    import org.eclipse.swt.*;
    import org.eclipse.swt.widgets.*;
    import org.eclipse.swt.graphics.Image;
    

    Add image on top left corner. It's important that you use "getResourceAsStream", you'll notice after export as Runnable jar:

    Display display = new Display();
    Shell shell = new Shell(display);
    InputStream inputImage = getClass().getResourceAsStream("/app/launcher.png");
    if (inputImage != null) {
        shell.setImage(new Image(display, inputImage));
    }
    

    User's home directory:

    String filterPath = System.getProperty("user.home");
    

    Get absolut pathname instead of filter-dependent pathname, which is wrong on other drive.

    String absolutePath = dialog.open();
    
    0 讨论(0)
  • 2020-11-28 09:17

    Since Swing emulates various L&F's, I guess your best bet would be to upgrade your JRE to the very latest and hope that the JFileChooser UI has been updated.

    0 讨论(0)
提交回复
热议问题