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.