File saved with JFileChooser showSaveDialog even o

2019-09-02 06:59发布

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.");
                }
}

3条回答
虎瘦雄心在
2楼-- · 2019-09-02 07:33

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.

查看更多
Bombasti
3楼-- · 2019-09-02 07:42

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.

查看更多
Summer. ? 凉城
4楼-- · 2019-09-02 07:56

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.");
    }
}
查看更多
登录 后发表回答