By
using DirectoryDialog widget, user can able to navigate through the file system
and select a file.
Ex:
String[]
filterExtensions = { "*.txt", "*.doc", "*.*"};
FileDialog
fileDialog = new FileDialog(shell, SWT.OPEN);
fileDialog.setText("FileDialog
Demo");
fileDialog.setFilterPath("C:/");
fileDialog.setFilterExtensions(filterExtensions);
String
selectedFile = fileDialog.open();
Following
is the complete working application.
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 Test { private static void addWidgetsToShell(Display display, Shell shell) { String[] filterExtensions = { "*.txt", "*.doc", "*.*"}; FileDialog fileDialog = new FileDialog(shell, SWT.OPEN); fileDialog.setText("FileDialog Demo"); fileDialog.setFilterPath("C:/"); fileDialog.setFilterExtensions(filterExtensions); String selectedFile = fileDialog.open(); System.out.println("User selected : " + selectedFile); shell.dispose(); } public static void main(String[] args) { /* Instantiate Display object, it represents SWT session */ Display display = new Display(); /* * Define Shell, it represent a window, You can add more than one shell * to Display */ Shell shell = new Shell(display); shell.setSize(500, 500); addWidgetsToShell(display, shell); /* * Run the event dispatching loop until an exit condition occurs, which * is typically when the main shell window is closed by the user. */ while (!shell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } } /* Dispose the display */ display.dispose(); } }
No comments:
Post a Comment