Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

save file dialog

Posted on 2001-06-24
4
Medium Priority
?
607 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 300 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses

688 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