An SWT FileDialog Selected Directories Only in Swing

人走茶凉 提交于 2019-12-11 19:44:20

问题


I have a swing app that I would like to use Windows7/Vista style FileDialogs and have found a reasonable solution using SWT in conjunction with swing: Does Swing support Windows 7-style file choosers?

However, now I'm trying to get this same dialog to accept only directories (the "Select Folder" button instead of the "Open" button).

I do not want to use the typical DirectoryDialog:



I want to use the Dialog with favorites on the left, address bar on the top, and the ability to select folders:

Anyone know how to accomplish this?

Replies are greatly appreciated.


回答1:


Baz already said it: It is not possible to get this dialog using SWT. To answer your question about other frameworks: I believe there are plenty, for example you could use Jide. You do not get the dialog you want, but at least you get an enhanced version (FolderChooser) with few advantages:

  • Convenience-Buttons (Desktop, My Documents, ...)
  • Delete/Create new directory
  • Address bar

And the best of all this: You get it for free, cause it's in the "Common Layer". You can try the FolderChooser by launching the Demo-WebStart-Project.




回答2:


It's kind of a hack:

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

public class Demo{

    public static void main(String [] args) {
        Display display = new Display();
        Shell shell = new Shell(display);  
        FileDialog dialog = new FileDialog(shell, SWT.OPEN | SWT.MULTI);
        dialog.setFilterPath("c:\\");

        //The extension doen't excist!
        dialog.setFilterExtensions(new String[] {"xyz"});
        //You can also use " ";

        dialog.open();
        shell.close();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) display.sleep();
        }
        display.dispose();
    }
}

I tryed it out and I think, it works well!



来源:https://stackoverflow.com/questions/17752050/an-swt-filedialog-selected-directories-only-in-swing

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!