Java Tutorial/SWT/FileDialog

Материал из Java эксперт
Перейти к: навигация, поиск

17. Displaying the Open or Save File Dialog

FileDialog Constants

ConstantDescriptionSWT.OPENCreates a dialog for opening a single file. This is the default.SWT.MULTICreates a dialog for opening multiple files.SWT.SAVECreates a dialog for saving a file.


17. File Save Dialog: filter name, filter extensions, filter path and file name

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Shell;
public class FileSaveDialogFilterNameFilterExtensions {
  public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    FileDialog dialog = new FileDialog(shell, SWT.SAVE);
    dialog.setFilterNames(new String[] { "Batch Files", "All Files (*.*)" });
    dialog.setFilterExtensions(new String[] { "*.bat", "*.*" }); // Windows
                                                                  // wild cards
    dialog.setFilterPath("c:\\"); // Windows path
    dialog.setFileName("yourFile.bat");
    System.out.println("Save to: " + dialog.open());
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }
    display.dispose();
  }
}





17. Getting the Selected File or Files

import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Shell;
public class FileDialogMultipleFileNameSelection {
  public static void main(String[] args) {
    Display display = new Display();
    final Shell shell = new Shell(display);
    FileDialog dlg = new FileDialog(shell, SWT.MULTI);
    Collection files = new ArrayList();
    if (dlg.open() != null) {
      String[] names = dlg.getFileNames();
      for (int i = 0, n = names.length; i < n; i++) {
        StringBuffer buf = new StringBuffer(dlg.getFilterPath());
        if (buf.charAt(buf.length() - 1) != File.separatorChar)
          buf.append(File.separatorChar);
        buf.append(names[i]);
        files.add(buf.toString());
      }
    }
    System.out.println(files);
    display.dispose();
  }
}





17. Open an Open FileDialog

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Shell;
public class FileDialogOpenSelectedName {
  public static void main(String[] args) {
    Display display = new Display();
    final Shell shell = new Shell(display);

    FileDialog dlg = new FileDialog(shell, SWT.OPEN);
    String fileName = dlg.open();
    if (fileName != null) {
      System.out.println(fileName);
    }

    
    display.dispose();
  }
}





17. Set FilterExtensions for FileDialog

import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Shell;
public class FileDialogFilterExtensions {
  public static void main(String[] args) {
    final Display display = new Display();
    final Shell shell = new Shell(display);
    
    
    FileDialog dialog = new FileDialog(shell);
    dialog.setFilterExtensions(new String[] { "*.ico", "*.gif", "*.*" });
    String name = dialog.open();
    
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }
    display.dispose();
  }
}





17. Specifying File Types and Extensions

setFilterNames(String[] names);
setFilterExtensions(String[] extensions);





17. Specifying the Starting Directory and File Name

FileDialog"s setFilterPath() method allows you to specify the initial directory for the dialog.



import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Shell;
public class FileDialogStartingDirectoryFileName {
  public static void main(String[] args) {
    Display display = new Display();
    final Shell shell = new Shell(display);
    FileDialog dlg = new FileDialog(shell, SWT.OPEN);
    dlg.setFilterPath("C:/");
    String fileName = dlg.open();
    if (fileName != null) {
      System.out.println(fileName);
    }
    display.dispose();
  }
}





17. Suggest a file name to use for saving a file by prefilling the entry field in the Save dialog box

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Shell;
public class FileDialogSaveDefaultFileName {
  public static void main(String[] args) {
    Display display = new Display();
    final Shell shell = new Shell(display);
    FileDialog dlg = new FileDialog(shell, SWT.SAVE);
    dlg.setFileName("defaultName");
    String fileName = dlg.open();
    if (fileName != null) {
      System.out.println(fileName);
    }
    display.dispose();
  }
}