Java Tutorial/SWT/FileDialog
Содержание
- 1 17. Displaying the Open or Save File Dialog
- 2 17. File Save Dialog: filter name, filter extensions, filter path and file name
- 3 17. Getting the Selected File or Files
- 4 17. Open an Open FileDialog
- 5 17. Set FilterExtensions for FileDialog
- 6 17. Specifying File Types and Extensions
- 7 17. Specifying the Starting Directory and File Name
- 8 17. Suggest a file name to use for saving a file by prefilling the entry field in the Save dialog box
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
<source lang="java">
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(); }
}</source>
17. Getting the Selected File or Files
<source lang="java">
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(); }
}</source>
17. Open an Open FileDialog
<source lang="java">
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(); }
}</source>
17. Set FilterExtensions for FileDialog
<source lang="java">
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(); }
}</source>
17. Specifying File Types and Extensions
<source lang="java">
setFilterNames(String[] names); setFilterExtensions(String[] extensions);</source>
17. Specifying the Starting Directory and File Name
FileDialog"s setFilterPath() method allows you to specify the initial directory for the dialog.
<source lang="java">
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(); }
}</source>
17. Suggest a file name to use for saving a file by prefilling the entry field in the Save dialog box
<source lang="java">
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(); }
}</source>