Solved

save file dialog

Posted on 2001-06-24
4
605 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

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…
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
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 will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

615 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