Home
Categories
Dictionnary
Download
Project Details
Changes Log
FAQ
License

File picker



The FilePicker class is available in all scripts to create a file picker.

FilePicker API

The FilePicker has the following interface:

public class FilePicker


Modifier and Type Method and Description
boolean appendFilters(String description, String extensions)
Add a file filter to the FilePicker
File getFile()
Return the selected File
void init(Object parent, String mode)
set the mode of the FilePicker, usable in all scripts, including Javascript scripts. The parent can be a XULDocument or a XULWidget, or even be null
void init(XULWidget parent, String title, String mode)
Same method for non Javascript scripts
int show()
Show the FilePicker chooser an return the value

Initialization

The FilePicker.init(Object, String, String) and FilePicker.init(XULWidget, String, String) methods allow to initialize the FilePicker.

The parent can be a XULDocument or a XULWidget, or even be null.

The String mode can be:
  • "openMode" to set the FilePicker in Open Mode
  • "saveMode" to set the FilePicker in Save Mode
For example (in Javascript):

      var filepicker = new FilePicker();
      filepicker.init(document, "openMode");    

Append filters

To append a filter, you have to use the FilePicker.appendFilters(String, String) method.
  • The first parameter is the description of the filter (for example "XUL Files")
  • The first parameter is the specification of the filter (for example "*.xul; *.xml")
For example (in Javascript):

      var filepicker = new FilePicker();
      filepicker.appendFilters("The XUL files", "*.xul");      

In Groovy:

      FilePicker filepicker = new FilePicker();
      filepicker.appendFilters("The XUL files", "*.xul");      

Example

In Javascript):

      <window title="FilePicker" width="400" height="400" 
              xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
         <script>
            function open() {
              var filepicker = new FilePicker();
              var parent = document.getElementById("open");
              filepicker.appendFilters("The XUL files", "*.xul");
              filepicker.init(parent, "Select File", "modeOpen");
              var ret = filepicker.show();
              if (ret == filepicker.returnOK) { 
                var text = helper.getText(filepicker.file);
                var textbox = document.getElementById("text");
                textbox.value = text;
              }
            }   
         </script>
         <menubar>      
            <menu label="File">
               <menupopup>
                  <menuitem id="open" label="Open" oncommand="open()"/>
               </menupopup>
            </menu>
         </menubar>        
         <textbox id="text" multiline="true" value=""  cols="30" rows="20"/>  
      </window>

See also


Categories: scripts

Copyright 2008-2020 Herve Girod. All Rights Reserved. Documentation and source under the LGPL licence