Solved

save file dialog

Posted on 2001-06-24
4
602 Views
Last Modified: 2008-02-01
Hi,
I'm using 'JFileChooser' dialog in order to save file to the file system, however if the user chooses to save a file with a name that already exists in the file system the file is overwrite.
Is there a dialog, which ask the user if he/she want to overwrite the file?

Thanks,
Shushan
0
Comment
Question by:Shushan
4 Comments
 
LVL 2

Expert Comment

by:dbaora
ID: 6222677
Hi,
you can use JOptionPanel.showConfirmDialog
regards,
dbaora
0
 

Author Comment

by:Shushan
ID: 6222746
Hi,
The JOptionPanel.showConfirmDialog open a dialog with a given massage, but I want a dialog which will first check if there is already a file in the file system with the given name and only then will open the dialog.

I will describe the scenario I want:
 1. User ask to save file
 2. Open the JfileChooser with showSaveDialog option
 3. User choose the name for the file
 4. Check to see if there is already file with the given   name
   4.1.     If no ? save the file
   4.2.     Else ? open a confirm dialog

So what I'm asking is if Java has implement a dialog which will do step 4 for me
0
 

Accepted Solution

by:
kainhart earned 100 total points
ID: 6222994
No I don't believe there is at least in the avialable Java API. Such a dialog would not be hard to create or simulate. One option would be to use the following method

public int showDialog(Component parent,
                      String approveButtonText)

for the button text, use the string "Save" and after the dialog is is done with, check the file that was chosen by the dialog and check if it exists, if not write the data to the file selected, else ask the user then either overwrite the file or not depending on what the user picked when you asked. To the user it will look as if they just used a SaveDialog when it was just a general JFileChooser Dialog.

Good luck
0
 
LVL 1

Expert Comment

by:Ecks
ID: 7265741
I just put this piece of code together... seems to work. :)

if (file.exists()) {
  int act = JOptionPane.showConfirmDialog(null, file.toString() + " exists.  Overwrite?");

  while (act == JOptionPane.NO_OPTION) {
    state = chooser.showSaveDialog(null);
    act = JOptionPane.showConfirmDialog(null, file.toString() + " exists.  Overwrite?");
  }

  if (act == JOptionPane.CANCEL_OPTION)
    file = null;
}
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…

789 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question