Does Swing support Windows 7-style file choosers?

前端 未结 10 2046
星月不相逢
星月不相逢 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: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() {
        }
    }
    

提交回复
热议问题