Search code examples
javaswingfiledialog

how to open Filedialog in desired directory


I have two buttons both open filedialog what i want both button should always open their related directories in filedialog when they press.In my case filedialog keep the last open directory which i don't want.


Solution

  • You didn't name the used framework, but assuming, it's Swing, JFileChooser has some constructors that allow to set the initial path:

    JFileChooser(File currentDirectory)
          Constructs a JFileChooser using the given File as the path.
    

    For FileDialog from the AWT framework, use a setter to set the directory before displaying the dialog

    setDirectory(String dir)
          Sets the directory of this file dialog window to be the specified directory.