Question

I have a file saved that works fine apart from one problem. When the cancel button is pressed a copy of the file is saved in the java directory. This only happens when the cancel button is pressed, if the save button is used the file ends up where the user selects. How can I stop this happening so when the cancel button is pressed nothing is saved anywhere?

My code is below, all help appreciated. Thanks

    // Save dialog
private void savePlaylist() {
JFileChooser savePlaylistDialog = new JFileChooser();
                savePlaylistDialog.setSelectedFile(new File(newPlaylistNameTxt.getText() + ".txt"));
                savePlaylistDialog.showSaveDialog(playlistDialogs);
                File savePlaylist = savePlaylistDialog.getSelectedFile();

                try {
                    outFile = new PrintWriter(new FileWriter(savePlaylist));
                    outFile.println(newPlaylistInformationTxt.getText());
                    outFile.close();

                    // Plays a sound when play() is called (edited from Bombard)
                    try {
                        Clip saveButtonSound = AudioSystem.getClip();
                        AudioInputStream ais = AudioSystem.getAudioInputStream(new File("Tri-tone.wav"));
                        saveButtonSound.open(ais);
                        saveButtonSound.start();
                    } catch (Exception ex) {
                        ex.printStackTrace();
                    }
                } catch (Exception ex) {
                    JOptionPane.showMessageDialog(null, "File could not be written, try again.");
                }
}
Was it helpful?

Solution

savePlaylistDialog.showSaveDialog(playlistDialogs);

That method call above returns an int. You need to check its value - if the user clicked on the Save button, it would return JFileChooser.ACCEPTED_OPTION. In this case, you are taking the return value (which could be accepted/save or cancel), ignoring it, and proceeding to write the data to disk anyway.

OTHER TIPS

Here is the fixed code I used:

    // Save dialog
    private void savePlaylist() {
    JFileChooser savePlaylistDialog = new JFileChooser();
    savePlaylistDialog.setSelectedFile(new File(newPlaylistNameTxt.getText() + ".txt"));
    int status = savePlaylistDialog.showSaveDialog(playlistDialogs);

    try {
        if (status == JFileChooser.APPROVE_OPTION) {
            //User has pressed save button

            File savePlaylist = savePlaylistDialog.getSelectedFile();

            outFile = new PrintWriter(new FileWriter(savePlaylist));
            outFile.println(newPlaylistInformationTxt.getText());
            outFile.close();

            // Plays a sound when play() is called (edited from Bombard)
            try {
                Clip saveButtonSound = AudioSystem.getClip();
                AudioInputStream ais = AudioSystem.getAudioInputStream(new File("Tri-tone.wav"));
                saveButtonSound.open(ais);
                saveButtonSound.start();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        } else if (status == JFileChooser.CANCEL_OPTION) {
            // User has pressed cancel button
        }
    } catch (Exception ex) {
        JOptionPane.showMessageDialog(null, "File could not be written, try again.");
    }
}

showSaveDialog should return whether the user canceled or not and you code shoul act accordingly. At the moment you save no matter what the user did in the save dialog.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top