JFileChooser: How to stop autofill file name after clicking on home directory?

This is what I’m talking about:

When you click on the home directory at the top (top red rectangle), it will auto-fill the file name textbox with the home directory absolute path (bottom red rectangle). How do you stop that from auto-filling the textbox?

If possible, if I extend JFileChooser, how do I override that auto-filling in the subclass?

This is theoretically LookAndFeel-specific, even though most LAFs probably handle it the same way. If you’re set on a specific LookAndFeel (e.g. Metal) across all OSes, set a custom LookAndFeel overridden to not insert the file name when going home:


public class MyFileChooserUI extends MetalFileChooserUI {

   public static ComponentUI createUI(JComponent c) {
      return new MyFileChooserUI((JFileChooser)c);
   }

   public MyFileChooserUI(JFileChooser chooser) {
      super(chooser);
   }

   @Override
   public void setFileName(String filename) {
      if (new File(filename).equals(getFileChooser().getFileSystemView().getHomeDirectory())) {
         super.setFileName("");
      }
      super.setFileName(filename);
   }

}

This will backfire if the user meant to choose the home directory.

What this workaround is doing is just preventing the home directory’s path from being entered into the text field; it shouldn’t make the “Home” button not work (the chooser will still navigate to the Home directory in its list/details view).

A side effect might be (untested) if the user actually types in their home directory path and hits Enter, that text will be cleared out as opposed to sticking around (IIRC, JFileChooser would keep the directory name entered when the user manually types something and hits Enter, though to me that’s ridiculous. But maybe they’ve fixed that now. Though again, such behavior is likely LAF-dependent). I left that testing as an exercise for the OP. :slight_smile:

Been thinking long and hard on this. Haven’t found a satisfied solution for it. I have settled on this for now.


					if (!editor.drawingBoardPanel.hasBitmap()) {
						JOptionPane.showMessageDialog(null, "No created maps to save.");
						break;
					}
					JFileChooser chooser = new JFileChooser();
					chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
					chooser.setCurrentDirectory(lastSavedDirectory);
					chooser.setFileFilter(new FileNameExtensionFilter("PNG files", "png"));
					chooser.setVisible(true);
					int result = chooser.showSaveDialog(null);
					if (result == JFileChooser.APPROVE_OPTION) {
						try {
							BufferedImage img = editor.drawingBoardPanel.getMapImage();
							if (img != null) {
								File file = chooser.getSelectedFile();
								String filename = file.getName();
								while (filename.endsWith(".png"))
									filename = filename.substring(0, filename.length() - ".png".length());
								this.lastSavedDirectory = chooser.getCurrentDirectory();
								ImageIO.write(img, "png", new File(this.lastSavedDirectory.getAbsolutePath() + "\\" + filename + ".png"));
							}
						}
						catch (IOException e) {
							e.printStackTrace();
						}
					}

Full source code here.

This makes JFileChooser to ignore the home directory upon invoking it, which will temporary clear the file name text field form. I would say that is probably enough to let the users navigate to where they wanted to save the data at. And stop from there.