Swing是否支持Windows 7风格的文件选择器? [英] Does Swing support Windows 7-style file choosers?

查看:153
本文介绍了Swing是否支持Windows 7风格的文件选择器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚将一个标准的打开文件对话框添加到我正在编写的小型桌面应用中,基于)打开这个对话框。以下是对SWT的修改 FileDialog片段使用open而不是save对话框。我知道这不是你想要的,但你可以把它隔离到一个实用程序类,并将swt.jar添加到你的类路径中以实现这个功能。

  import org.eclipse.swt。*; 
import org.eclipse.swt.widgets。*;

public class SWTFileOpenSnippet {
public static void main(String [] args){
display display = new Display();
Shell shell = new Shell(显示);
//不显示shell。
//shell.open();
FileDialog dialog = new FileDialog(shell,SWT.OPEN | SWT.MULTI);
String [] filterNames = new String [] {All Files(*)};
String [] filterExtensions = new String [] {*};
String filterPath =c:\\;
dialog.setFilterNames(filterNames);
dialog.setFilterExtensions(filterExtensions);
dialog.setFilterPath(filterPath);
dialog.open();
System.out.println(Selected files:);
String [] selectedFileNames = dialog.getFileNames();
for(String fileName:selectedFileNames){
System.out.println(+ fileName);
}
shell.close();
while(!shell.isDisposed()){
if(!display.readAndDispatch())display.sleep();
}
display.dispose();
}
}


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 window that looks like this:

but I would prefer to have a window that looks like this:

In other words, I want my file chooser to have Windows Vista/Windows 7's style, not Windows XP's. Is this possible in Swing? If so, how is it done? (For the purposes of this question, assume that the code will be running exclusively on Windows 7 computers.)

解决方案

It does not appear this is supported in Swing in Java 6.

Currently, the simplest way I can find to open this dialog is through SWT, not Swing. SWT's FileDialog (javadoc) brings up this dialog. The following is a modification of SWT's FileDialog snippet to use an open instead of save dialog. I know this isn't exactly what you're looking for, but you could isolate this to a utility class and add swt.jar to your classpath for this functionality.

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

public class SWTFileOpenSnippet {
    public static void main (String [] args) {
        Display display = new Display ();
        Shell shell = new Shell (display);
        // Don't show the shell.
        //shell.open ();  
        FileDialog dialog = new FileDialog (shell, SWT.OPEN | SWT.MULTI);
        String [] filterNames = new String [] {"All Files (*)"};
        String [] filterExtensions = new String [] {"*"};
        String filterPath = "c:\\";
        dialog.setFilterNames (filterNames);
        dialog.setFilterExtensions (filterExtensions);
        dialog.setFilterPath (filterPath);
        dialog.open();
        System.out.println ("Selected files: ");
        String[] selectedFileNames = dialog.getFileNames();
        for(String fileName : selectedFileNames) {
            System.out.println("  " + fileName);
        }
        shell.close();
        while (!shell.isDisposed ()) {
            if (!display.readAndDispatch ()) display.sleep ();
        }
        display.dispose ();
    }
} 

这篇关于Swing是否支持Windows 7风格的文件选择器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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