Class WindowsFileChooser

java.lang.Object
anlavn.ui.filechooser.WindowsFileChooser

public class WindowsFileChooser extends Object
The native Windows file chooser dialog. Example: WindowsFileChooser fc = new WindowsFileChooser("C:\\"); fc.addFilter("All Files", "*"); fc.addFilter("Text files", "txt", "log", "xml", "css", "html"); fc.addFilter("Source code", "java", "c", "cpp", "cc", "h", "hpp"); fc.addFilter("Binary files", "exe", "class", "jar", "dll", "so"); if (fc.showOpenDialog(parent)) { File f = fc.getSelectedFile(); // do something with f } Note that although you can set the initial directory Windows will determine the initial directory according to the following rules (the initial directory is referred to as "lpstrInitialDir"): Windows 7: 1. If lpstrInitialDir has the same value as was passed the first time the application used an Open or Save As dialog box, the path most recently selected by the user is used as the initial directory. 2. Otherwise, if lpstrFile contains a path, that path is the initial directory. 3. Otherwise, if lpstrInitialDir is not NULL, it specifies the initial directory. 4. If lpstrInitialDir is NULL and the current directory contains any files of the specified filter types, the initial directory is the current directory. 5. Otherwise, the initial directory is the personal files directory of the current user. 6. Otherwise, the initial directory is the Desktop folder. Windows 2000/XP/Vista: 1. If lpstrFile contains a path, that path is the initial directory. 2. Otherwise, lpstrInitialDir specifies the initial directory. 3. Otherwise, if the application has used an Open or Save As dialog box in the past, the path most recently used is selected as the initial directory. However, if an application is not run for a long time, its saved selected path is discarded. 4. If lpstrInitialDir is NULL and the current directory contains any files of the specified filter types, the initial directory is the current directory. 5. Otherwise, the initial directory is the personal files directory of the current user. 6. Otherwise, the initial directory is the Desktop folder. Therefore you probably want to use an exe wrapper like WinRun4J in order for this to work properly on Windows 7. Otherwise multiple programs may interfere with each other. Unfortunately there doesn't seem to be a way to override this behaviour.
  • Field Details

    • selectedFile

      protected File selectedFile
    • currentDirectory

      protected File currentDirectory
    • filters

      protected ArrayList<String[]> filters
    • defaultFilename

      protected String defaultFilename
    • dialogTitle

      protected String dialogTitle
  • Constructor Details

    • WindowsFileChooser

      public WindowsFileChooser()
      creates a new file chooser
    • WindowsFileChooser

      public WindowsFileChooser(File currentDirectory)
      creates a new file chooser with the specified initial directory If the given file is not a directory the parent file will be used instead.
      Parameters:
      currentDirectory - the initial directory
    • WindowsFileChooser

      public WindowsFileChooser(String currentDirectoryPath)
      creates a new file chooser with the specified initial directory path
      Parameters:
      currentDirectoryPath - the initial directory path; may be null
  • Method Details

    • addFilter

      public void addFilter(String name, String... filter)
      add a filter to the user-selectable list of file filters
      Parameters:
      name - name of the filter
      filter - you must pass at least 1 argument, the arguments are the file extensions.
    • setTitle

      public void setTitle(String tname)
      set a title name
      Parameters:
      tname - of dialog
    • showOpenDialog

      public boolean showOpenDialog(Window parent)
      show the dialog for opening a file
      Parameters:
      parent - the parent window of the dialog
      Returns:
      true if the user clicked ok, false otherwise
    • showSaveDialog

      public boolean showSaveDialog(Window parent)
      show the dialog for saving a file
      Parameters:
      parent - the parent window of the dialog
      Returns:
      true if the user clicked ok, false otherwise
    • getSelectedFile

      public File getSelectedFile()
      returns the file selected by the user
      Returns:
      the selected file; null if the dialog was canceled or never shown
    • getCurrentDirectory

      public File getCurrentDirectory()
      returns the current directory This is always the parent directory of the chosen file, even if you enter an absolute path to a file that doesn't exist in the current directory.
      Returns:
      the current directory
    • setDefaultFilename

      public void setDefaultFilename(String defaultFilename)