Class FileChooserBox


  • public class FileChooserBox
    extends Window
    A class that shows all folders from a startup one to allow the user select a file or a folder. Here's a sample of how to use it:
       try
       {
          FileChooserBox w = new FileChooserBox("Select the folder",new String[]{"  This one  "," Cancel "},
             new FileChooserBox.Filter()
             {
                public boolean accept(File f) throws IOException
                {
                   return f.isDir(); // will only list folders. you may filter by other file types too
                }
             });
          w.mountTree(Settings.appPath,1);
          w.popup();
          return w.getPressedButtonIndex() == 0 ? w.getAnswer() : null;
       }
       catch (IOException e)
       {
          return null;
       }
     
    The tree is mounted on demand to speedup the process.

    Here's a list of customizations you can do:

    • You can set a path to be selected initially by setting the initialPath property.
    • Set the defaultButton property to allow the selection of an item doing a double-click on it.
    • Field Detail

      • tree

        protected Tree tree
      • lastSelected

        protected Node lastSelected
      • defaultButtonCaptions

        public static java.lang.String[] defaultButtonCaptions
        The default button captions: " Select " and " Cancel ". You can localize them if you want.
      • msgVolume

        public static java.lang.String msgVolume
        The "Volume: " label that's placed before the volume combo. You can localize it if you want.
      • msgRefresh

        public static java.lang.String msgRefresh
        The " Refresh " button that's placed after the volume combo. You can localize it if you want.
      • msgInvalidVolumeTitle

        public static java.lang.String msgInvalidVolumeTitle
        The title of a message box that appears if the user tries to access a volume and an error is issued by the operating system. Defaults to "Error". You can localize it if you want.
      • previewTitle

        public static java.lang.String previewTitle
        The " Preview " button title. You can localize it if you want.
      • msgInvalidVolumeMessage

        public static java.lang.String msgInvalidVolumeMessage
        The body of a message box that appears if the user tries to access a volume and an error is issued by the operating system. Defaults to "Unable to read the contents of the selected volume. Make sure the volume is mounted and you have enough privileges to query its contents.". You can localize it if you want.
      • buttonCaptions

        protected java.lang.String[] buttonCaptions
      • selectedIndex

        protected int selectedIndex
      • btRefresh

        protected Button btRefresh
      • showInitialPathOnly

        public boolean showInitialPathOnly
        On Win32, we show by default the root drive and expand to the default path. Set this to false to show the initial path only.
        Since:
        TotalCross 3.2
      • fileCount

        public int fileCount
      • multipleSelection

        public boolean multipleSelection
        Set to true to allow multiple selections using a Check drawn before the nodes.
        Since:
        TotalCross 1.15
      • initialPath

        public java.lang.String initialPath
        Selects a file that is in this tree.
        Since:
        TotalCross 1.25
      • defaultButton

        public int defaultButton
        The button index that will be choosen if the user makes a double-click in an item. Usually you set this to the index of the "Ok" button.
      • PREVIEW_HEIGHT

        public static final int PREVIEW_HEIGHT
        The preview height in percentage of the total height. Defaults to 30.
        See Also:
        Constant Field Values
      • showPreview

        public boolean showPreview
        Set to true to use a preview window to show photo thumbnails
      • newestFirst

        public boolean newestFirst
        Set to true to sort the list showing the newest files first, instead of alphabetical order
    • Constructor Detail

      • FileChooserBox

        public FileChooserBox​(java.lang.String caption,
                              java.lang.String[] buttonCaptions,
                              FileChooserBox.Filter ff)
        Constructs a file chooser with the given parameters.
        Parameters:
        caption - The caption to be displayed in the title
        buttonCaptions - The button captions that will be used in the PushButtonGroup
        ff - The Filter. Pass null to accept all files.
      • FileChooserBox

        public FileChooserBox​(FileChooserBox.Filter ff)
        Constructs a file chooser with "Select a file" as the window title, and "Select" and "Cancel" buttons.
        Parameters:
        ff - The Filter. Pass null to accept all files.
    • Method Detail

      • onPopup

        protected void onPopup()
        Description copied from class: Window
        Placeholder called imediatly before the popup began. The default implementation does nothing.
        Overrides:
        onPopup in class Window
      • mountTree

        @Deprecated
        public void mountTree​(java.lang.String filePath,
                              int volume)
                       throws IOException
        Deprecated.
        Throws:
        IOException
      • mountTree

        public void mountTree​(java.lang.String filePath)
                       throws IOException
        Call this method to mount the tree, starting from the given path and volume.
        Parameters:
        filePath - The root from where the tree will be mounted.
        Throws:
        IOException
      • onEvent

        public void onEvent​(Event e)
        Description copied from class: Control
        Called to process key, pen, control and other posted events.
        Overrides:
        onEvent in class Control
        Parameters:
        e - the event to process
        See Also:
        Event, KeyEvent, PenEvent
      • getPressedButtonIndex

        public int getPressedButtonIndex()
        Returns the button index used to close this window.
      • getAnswer

        public java.lang.String getAnswer()
        Returns the path choosen by the user. If using the default captions and the cancel button was pressed, returns null. The filename is returned with normal (/) slashes; the path also ends with a slash. If multipleSelections is on, a list of paths, separated by comma (,) is returned.
      • getTree

        public Tree getTree()
        Returns the tree.