Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Save Dialog problem

fh791fh791 Posts: 35Member
Hi,
I have a problem with JFileChooser.
I want to use save dialog of JFileChooser.I expected the dialog
automatically shows a message if the selected file exists and ask
the user if he wants to overwrite it. But (sadly) it does not!
Can someone help??? Is there anything should be done with the
dialog (like setting a property) or I have to write some codes
for it (such as getting the selected file name from the dialog and
checking its existence ???)

Thanks,

Comments

  • arb123arb123 Posts: 696Member
    : Hi,
    : I have a problem with JFileChooser.
    : I want to use save dialog of JFileChooser.I expected the dialog
    : automatically shows a message if the selected file exists and ask
    : the user if he wants to overwrite it. But (sadly) it does not!
    : Can someone help??? Is there anything should be done with the
    : dialog (like setting a property) or I have to write some codes
    : for it (such as getting the selected file name from the dialog and
    : checking its existence ???)
    :
    : Thanks,
    :
    :

    This can be handled like this:
    [code]
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.File;

    import javax.swing.JFileChooser;
    import javax.swing.JFrame;
    import javax.swing.JMenu;
    import javax.swing.JMenuBar;
    import javax.swing.JMenuItem;
    import javax.swing.JOptionPane;

    public class SaveFrame extends JFrame {

    private JMenuBar menuBar = new JMenuBar();
    private JMenu menu = new JMenu("File");
    private JMenuItem menuItem = new JMenuItem("Save...");

    public SaveFrame() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(200, 100);

    menu.add(menuItem);
    menuBar.add(menu);
    setJMenuBar(menuBar);

    menuItem.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent arg0) {
    onSave();
    }
    });
    }

    protected void onSave() {
    File saveFile = null;
    JFileChooser fileChooser = new JFileChooser();
    while (true) {
    int choice = fileChooser.showSaveDialog(this);
    if (choice == JFileChooser.APPROVE_OPTION) {
    File chosen = fileChooser.getSelectedFile();
    if (!chosen.exists()) {
    saveFile = chosen;
    break;
    } else {
    int confirm =
    JOptionPane.showConfirmDialog(
    this,
    "Overwrite file? " + chosen.getName());
    if (confirm == JOptionPane.OK_OPTION) {
    saveFile = chosen;
    break;
    } else if (confirm == JOptionPane.NO_OPTION) {
    continue;
    }
    break;
    }
    } else {
    break;
    }
    }

    System.err.println("Save: " + saveFile);
    }

    public static void main(String[] args) {
    new SaveFrame().show();
    }
    }
    [/code]

    ---------------------------------
    [size=1](Its just my sig)
    HOWTO ask questions: http://catb.org/~esr/faqs/smart-questions.html[/size]

Sign In or Register to comment.