title: Setting the selectedFile to a directory on a FileChooser in Java
body: <p>Long time developer, Java newbie.</p>
  
  <p>I'm using a JFileChooser object with a SelectionMode for DIRECTORIES_ONLY.  It is launched from a Browse JButton next to a JTextField where I am storing the directory after a selection has been made.</p>
  
  <p>My question is this:  When I try to select a directory by clicking on my Browse button a second time how can I get it to open with the previously selected directory selected in the File Chooser?  Allow me to expand, I have no problem setting the current directory to the parent of the value in my JTextfield or setting the selected file to the value in my JTextfield, but in either case the actual final branch of the directory I have chosen previously is not selected.</p>
  
  <p>If I do a similar exercise with a file chooser with a SelectionMode of FILES_ONLY; when I set the selected file to the previously selected file it opens to the parent directory and the file is highlighted in the file chooser and it's full path appears in the text field at the bottom of the file chooser.</p>
  
  <p>Here is a stripped down version of my code that should demonstrate what I'm talking about.</p>
  
  <pre><code>import java.awt.event.*;
  import java.awt.*;
  import java.io.*;
  import java.lang.reflect.*;
  import java.nio.charset.*;
  import java.nio.file.*;
  import java.util.*;
  import javax.swing.*;
  import javax.swing.plaf.*;
  import javax.swing.filechooser.*;
  
  public class myFrame extends JFrame {
  public myFrame() {
  
  JLabel lblProjDir = new JLabel("Project Directory:");
  JTextField txtProjDir = new JTextField(System.getProperty("user.home"));
  JButton btnBrowse= new JButton("...");
  JButton btnOK = new JButton("OK");
  
  add(lblProjDir);
  add(txtProjDir);
  add(btnBrowse);
  add(btnOK);
  
  btnBrowse.addActionListener( new ActionListener(){
  public void actionPerformed( ActionEvent e ){
  JFileChooser fileDialog = new JFileChooser();
  fileDialog.setSelectedFile( new java.io.File(txtProjDir.getText()));
  fileDialog.setDialogTitle("Set Working Project Directory");
  fileDialog.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
  fileDialog.setAcceptAllFileFilterUsed(false);
  
  if (fileDialog.showOpenDialog(btnBrowse) == JFileChooser.APPROVE_OPTION) {
  txtProjDir.setText("" + fileDialog.getSelectedFile());
  } } } );
  
  btnOK.addActionListener( new ActionListener(){
  public void actionPerformed( ActionEvent e ){
  setVisible(false);
  //Do some other actions here with data collected
  dispose();
  }
  } );
  
  setSize(200, 200);
  setMinimumSize(new Dimension(200,200));
  setLocationRelativeTo(null);
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  setVisible(true);
  }
  
  public static void main(String[] args) {
  SwingUtilities.invokeLater(new Runnable() {
  @Override
  public void run() {
  new myFrame();
  }
  } );
  }
  }
  </code></pre>
  
  <p>Any help would be greatly appreciated. :)</p>