File Picker

From Apache OpenOffice Wiki
Jump to: navigation, search

A file picker supports the service and may depict a file-open or a file-save dialog in all conceivable facets. supports a great variety of filters. These may be applied to the file picker by means of the filter manager. Filters also affect the list of files displayed by the dialog and enable the file picker to append the file extension automatically. The names of the filters and their titles may be queried programmatically from the registry or - much easier like in the coding example below - be retrieved from Framework/Article/Filter. The following listing illustrates how to customize and raise a file-save dialog and query the result afterwards. The result is a file URL pointing to the location where a file is to be stored.

  public String raiseSaveAsDialog() {
  String sStorePath = ""; 
  XComponent xComponent = null;
  try {
      // the filepicker is instantiated with the global Multicomponentfactory...
      Object oFilePicker = m_xMCF.createInstanceWithContext("", m_xContext);
      XFilePicker xFilePicker = (XFilePicker) UnoRuntime.queryInterface(XFilePicker.class, oFilePicker);
      // the defaultname is the initially proposed filename..
      // set the initial displaydirectory. In this example the user template directory is used
      Object oPathSettings = m_xMCF.createInstanceWithContext("",m_xContext); 
      XPropertySet xPropertySet = (XPropertySet), oPathSettings); 
      String sTemplateUrl = (String) xPropertySet.getPropertyValue("Template_writable");
      // set the filters of the dialog. The filternames may be retrieved from
      XFilterManager xFilterManager = (XFilterManager) UnoRuntime.queryInterface(XFilterManager.class, xFilePicker);
      xFilterManager.appendFilter("OpenDocument Text Template", "writer8_template"); 
      xFilterManager.appendFilter("OpenDocument Text", "writer8");
      // choose the template that defines the capabilities of the filepicker dialog
      XInitialization xInitialize = (XInitialization) UnoRuntime.queryInterface(XInitialization.class, xFilePicker);
      Short[] listAny = new Short[] { new Short(};
      // add a control to the dialog to add the extension automatically to the filename...
      XFilePickerControlAccess xFilePickerControlAccess = (XFilePickerControlAccess) UnoRuntime.queryInterface(XFilePickerControlAccess.class, xFilePicker);
      xFilePickerControlAccess.setValue(, (short) 0, new Boolean(true));
      xComponent = (XComponent) UnoRuntime.queryInterface(XComponent.class, xFilePicker);
      // execute the dialog...
      XExecutableDialog xExecutable = (XExecutableDialog) UnoRuntime.queryInterface(XExecutableDialog.class, xFilePicker);
      short nResult = xExecutable.execute();
      // query the resulting path of the dialog...
      if (nResult =={
          String[] sPathList = xFilePicker.getFiles();
          if (sPathList.length > 0){
              sStorePath = sPathList[0]; 
  } catch ( exception) {
      //make sure always to dispose the component and free the memory!
      if (xComponent != null){
  return sStorePath;

The directory that the file dialog initially displays is set by the setDisplayDirectory() method. Of course it must be set as a file URL. If no directory is passed, the customized Work-directory of the office application is shown.

Next to the file picker service it is also possible to raise a folder picker implementing the service Unlike the file picker the folder picker only displays folders.

  /** raises a folderpicker in which the user can browse and select a path
   * @param _sDisplayDirectory the path to the directory that is initially displayed
   * @param _sTitle the title of the folderpicker
   * @return the path to the folder that the user has selected. if the user has closed 
   * the folderpicker by clicking the "Cancel" button
   * an empty string is returned
   * @see
  public String raiseFolderPicker(String _sDisplayDirectory, String _sTitle) {
  String sReturnFolder = "";
  XComponent xComponent = null;
  try {
      // instantiate the folder picker and retrieve the necessary interfaces...
      Object oFolderPicker = m_xMCF.createInstanceWithContext("", m_xContext);
      XFolderPicker xFolderPicker = (XFolderPicker) UnoRuntime.queryInterface(XFolderPicker.class, oFolderPicker);
      XExecutableDialog xExecutable = (XExecutableDialog) UnoRuntime.queryInterface(XExecutableDialog.class, oFolderPicker);
      xComponent = (XComponent) UnoRuntime.queryInterface(XComponent.class, oFolderPicker);
      // set the dialog title...
      // show the dialog...
      short nResult = xExecutable.execute();
      // User has clicked "Select" button...
      if (nResult =={
          sReturnFolder = xFolderPicker.getDirectory();
  }catch( Exception exception ) {
      //make sure always to dispose the component and free the memory!
      if (xComponent != null){
      // return the selected path. If the user has clicked cancel an empty string is 
      return sReturnFolder;
Content on this page is licensed under the Public Documentation License (PDL).
Personal tools
In other languages