Solved

save file dialog

Posted on 2001-06-24
4
604 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
runtime exception 2 67
Android development question 2 111
ArrayList Adding/settign data 3 38
Which is best Image Resizing Web service 11 42
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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses

752 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