Using the SWT FileDialog Class
If your application deals with files in any manner, you need a mechanism to ask the user for a filename (to open or save). You have no doubt seen examples of file open dialogs in your favorite development environment or text editor, and you know that they can be fairly complex. A file open dialog will contain many buttons, and a list box or tree view (or both) that enables the user to navigate through the filesystem.
Developing such a dialog from scratch would take many hours of
programming time. Fortunately, the SWT FileDialog
class does all the heavy lifting for you—all you need to do is
create an instance of FileDialog
and specify a few
parameters.
How do I do that?
FileDialog
works almost exactly like
MessageBox
, with just a few different settings to
work with. The most logical example of the use of
FileDialog
is to prompt the user for File Open and
File Save information, as demonstrated in Example 17-3.
Example 17-3. Using FileDialog
import org.eclipse.swt.widgets.*; import org.eclipse.swt.SWT; import org.eclipse.swt.events.SelectionAdapter; import org.eclipse.swt.events.SelectionEvent; import org.eclipse.swt.events.SelectionListener; import org.eclipse.swt.graphics.Image; public class FileDialogExample { Display d; Shell s; FileDialogExample( ) { d = new Display( ); s = new Shell(d); s.setSize(400,400); s.setImage(new Image(d, "c:\\icons\\JavaCup.ico")); s.setText("A MessageBox Example"); // create the menu system Menu m = new Menu(s,SWT.BAR); ...
Get SWT: A Developer's Notebook now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.