Getting complete JFileChooser input and preventing other actions

I have a class FileSaver

that extends the JFileChooser class so that I can manage file filters and validate input. Here is the code:

import java.awt.event.ActionEvent;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
import javax.swing.filechooser.FileNameExtensionFilter;

private class FileSaver extends JFileChooser {

    private String pngDescription = "*.png";
    private String jpgDescription = "*.png";

    public FileSaver() {
        FileNameExtensionFilter jpgFilter = new FileNameExtensionFilter(jpgDescription, "jpg");
        addChoosableFileFilter(new FileNameExtensionFilter(pngDescription, "png"));

    public void approveSelection() {
        boolean approveFileName = false;
        File newFile = getSelectedFile();
        if (newFile.getName().matches(".*[\\\\/?<>:|\"%*].*")) { // Regex which matches if invalid characters present
            JOptionPane.showMessageDialog(this, "The following characters are not allowed: \\ / ? < > : | \" % *");
        } else if (newFile.exists()) {
            int overwriteReturn = JOptionPane.showConfirmDialog(this, "A file with this name already exists, would you like to overwrite it?", "File exists", JOptionPane.YES_NO_OPTION);
            if (overwriteReturn == JOptionPane.YES_OPTION) { // If the user chose to overwrite the file
                approveFileName = true;
        } else {
            approveFileName = true;
        if (approveFileName) {
            try {
                if (newFile.createNewFile()) {
                    super.approveSelection(); // Allow the dialog to close and the showSaveDialog() method to return APPROVE_OPTION
            } catch (IOException e) {
                JOptionPane.showMessageDialog(this, "There was a problem saving the image; you might not have permission to save to the directory you chose.");



The only instance I want to use this class in is this method in another class:

public void someMethod() {
    FileSaver fileSaver = new FileSaver();
    int saveFileReturn = fileSaver.showSaveDialog(window);
    if (saveFileReturn == FileSaver.APPROVE_OPTION) {
        File newFile = fileSaver.getSelectedFile();


This is fine for most of the filenames entered, but if the filename contains a question mark ("?") Or an asterisk ("*"), then approveSelection()

a new file filter is not called and a new file filter is created (and the program appears to basically treat the file filter as its previous parameter) with the filename as a description. Is it possible to handle these filenames like any others (for example, the approveSelection()

selected file is also invoked )?

eg. does this image show the result of trying to save "nameWithQuestionMark"? in the desktop directory:

Save Dialog

It also seems that a filename containing a forward slash ("/") is treated as a path relative to the current directory, and therefore getSelectedFile().getName()

only returns the part of the filename after the forward slash (or removing the forward slash at the end of the filename) and the part before appending to the path to catalog. Is it possible to approveSelection()

get the full filename to check and not change the directory?

eg. Typing the filename "dir / name" in the / home directory should provide the selected file with an empty "/ home" and the name "dir / name" and store the contents of the filename field as "dir / name". Instead, the selected file path is "/ home / dir" and the name is "name" and the "name" field contains "name".


source to share

All Articles