Monday, 9 January 2017

SWT: DirectoryDialog

By using DirectoryDialog widget, user can able to navigate through the directory system.

Ex:
DirectoryDialog directoryDialog = new DirectoryDialog(shell);
directoryDialog.setText("DirectoryDialog Demo");
directoryDialog.setFilterPath("C:/Users");
String selectedDirectory = directoryDialog.open();

Following is the complete working application.
import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class Test {

 private static void addWidgetsToShell(Display display, Shell shell) {
  DirectoryDialog directoryDialog = new DirectoryDialog(shell);
  directoryDialog.setText("DirectoryDialog Demo");
  directoryDialog.setFilterPath("C:/Users");
  String selectedDirectory = directoryDialog.open(); 
  
  System.out.println("User selected : " + selectedDirectory);
  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();

 }
}


Run above application, you can able to see following window.



Previous                                                 Next                                                 Home

No comments:

Post a Comment