File saved with JFileChooser showSaveDialog even on 'Cancel'

▼魔方 西西 提交于 2019-12-11 15:59:00

问题


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

回答1:


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.




回答2:


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



回答3:


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.



来源:https://stackoverflow.com/questions/9079709/file-saved-with-jfilechooser-showsavedialog-even-on-cancel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!