How to get the path of a file with the Windows Explorer in java?

1

My problem is that when using the Java File Chooser I can not preview the photos I want to upload to the database and it is very important because I manage many. So I want to be able to use the windows explorer to do this because in it you can put large or very large icons and this would be very helpful. I hope your help I really need it.

    
asked by Arturo Carrillo Fernández 26.11.2016 в 18:27
source

1 answer

0

I think what you're looking for is the SWT's FileDialog , here is a example that clarifies its use a bit, in your case I understand that you want to use it to" Open "no for "Save" and I guess to handle several files, so do not forget to change the example code to:

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 [] {"Todos los archivos (*)"};
    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();
}

To modify the filters of accepted files you must modify the following lines, in this case we will allow only JPEG or PNG files:

String[] filterNames = new String [] {"JPEG (*.jpg)", "PNG (*.png)"};
String[] filterExtensions = new String [] {"*.jpg", "*.png"};

And this I have not tried but it should work also, it would offer 3 options, All the images, JPEG and PNG:

String[] filterNames = new String [] {"Todas las imagenes (*.jpg, *.png)", "JPEG (*.jpg)", "PNG (*.png)"};
String[] filterExtensions = new String [] {"*.jpg|*.png", "*.jpg", "*.png"};
    
answered by 26.11.2016 / 18:43
source