Solved

save file dialog

Posted on 2001-06-24
4
599 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
Comment Utility
Hi,
you can use JOptionPanel.showConfirmDialog
regards,
dbaora
0
 

Author Comment

by:Shushan
Comment Utility
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
Comment Utility
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
Comment Utility
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Performance issue while iterating with streams 5 39
countHi challenge 25 84
strCopies  challenge 17 73
bitbucket vs gitbucket 3 28
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
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 …
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

763 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now