Results 1 to 3 of 3
  1. #1
    Join Date
    Jul 2010
    Posts
    5
    Rep Power
    0

    Thumbs up SaveAs button click

    And in my code , it is working fine, but when i click save as button, save as dialog pops up and if i give an already existing file name, it throws an error dialog as "File already exists, do you want to replace?' and if i click CANCEL, the save as dialog again pops up and if i give the same file name which is already existing, it saves, without giving error dialog, that 'File name already exist?'


    <html>
    <p>
    map_saveas.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {

    System.out.println(mainFrame.getTitle());
    String title=mainFrame.getTitle();
    String s5="";
    if(title.indexOf("-")!=-1)
    {
    int Index=title.indexOf("-");
    s5=title.substring(0, Index);
    }
    else
    {
    s5=mainFrame.getTitle();
    }
    File wrkDir = new File("c:/nis/Projects/"+s5+"/");
    JFileChooser chooser = new JFileChooser();
    setFileChooserFont(chooser.getComponents());
    chooser.setCurrentDirectory(wrkDir);
    chooser.setDialogTitle("Save As");
    chooser.setAcceptAllFileFilterUsed(false);
    chooser.setFileFilter(new SaveMapfilter());
    int returnVal = chooser.showSaveDialog(null);
    File fil=chooser.getSelectedFile();

    String filename=fil.getName();


    String newfilename;
    int index=filename.indexOf(".");
    System.out.println(index);
    String filename1=fil.getName();
    //String s=form.getText(0);
    String s2=s5+"-"+filename1+".map";
    setTitle(s2);

    if(index==-1)
    {
    String s6="c:/nis/Projects/"+s5+"/"+filename;
    File f = new File(s6);
    try{
    if(f.mkdir())
    System.out.println("Directory Created");
    else
    System.out.println("Directory is not created");
    }catch(Exception e5){
    System.out.println(e5);
    }
    String sub=filename+".map";
    newfilename=sub;
    String s8="c:/nis/Projects/"+s5+"/"+filename+"/"+sub;
    fil=new File(s8);

    }else {


    String sub1=filename.substring(0,index);
    String sub2=sub1;
    String s6="c:/nis/Projects/"+s5+"/"+sub2;
    File f = new File(s6);
    try{
    if(f.mkdir())
    System.out.println("Directory Created");
    else
    System.out.println("Directory is not created");
    }catch(Exception e5){
    System.out.println(e5);
    }
    String sub=filename;
    newfilename=sub;
    String s8="c:/nis/Projects/"+s5+"/"+sub2+"/"+sub;
    fil=new File(s8);

    }



    if (returnVal == JFileChooser.APPROVE_OPTION) {
    BufferedWriter bwriter1=null;

    try {
    //*********************
    FileWriter fwriter = new FileWriter(fil);
    // FileWriter fwriter = new FileWriter(newfile);
    BufferedWriter bwriter = new BufferedWriter(fwriter);
    }catch(Exception exception)
    {System.out.println(exception);}
    // System.out.println(newfilename);
    try {
    FileWriter fwriter = new FileWriter(fil);
    // FileWriter fwriter = new FileWriter(newfile);
    BufferedWriter bwriter = new BufferedWriter(fwriter);
    for (int i=0;i<mappingTable.getRowCount();i++) {
    for (int j=0;j<mappingTable.getColumnCount();j++) {
    String s=String.valueOf(mappingTable.getValueAt(i,j));
    //System.out.println(s);
    int l=s.length();
    //System.out.println(l);
    if(s.equals("null"))
    {
    bwriter.write(" ");
    s=" ";
    //System.out.println(s);
    }
    // bwriter.write( String.valueOf(table.getValueAt(i,j))+"" );
    bwriter.write(s );
    bwriter.newLine();

    }
    //bwriter.newLine();
    }
    bwriter.close();
    } catch (IOException ioe){
    ioe.printStackTrace();
    }
    //****************
    //*********************
    // BufferedWriter bwriter1=null;
    // System.out.println(newfilename);
    try {
    FileWriter fwriter1 = new FileWriter(file,true);

    bwriter1 = new BufferedWriter(fwriter1);
    bwriter1.newLine();


    bwriter1.write(newfilename );

    // bwriter1.newLine();




    bwriter1.close();
    } catch (IOException ioe){
    ioe.printStackTrace();
    }


    //****************
    }


    }
    });
    int saveasactions=map_saveas.getActionListeners().leng th;
    if(saveasactions>1)
    {
    for(int i=0;i<(saveasactions-1);i++)
    {
    map_saveas.removeActionListener(map_saveas.getActi onListeners()[i]);
    }
    }
    </p>
    </html>

  2. #2
    Join Date
    Jul 2010
    Posts
    5
    Rep Power
    0

    Default

    Sir,


    post quick reply

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,193
    Rep Power
    19

Similar Threads

  1. How to show new window after click on button
    By javahoush in forum SWT / JFace
    Replies: 4
    Last Post: 12-29-2009, 09:25 AM
  2. How can I display on Button click?
    By ntagrafix in forum New To Java
    Replies: 3
    Last Post: 11-04-2009, 12:05 AM
  3. How we can open a file with a click in button
    By kostinio in forum AWT / Swing
    Replies: 2
    Last Post: 11-01-2009, 04:46 PM
  4. deselecting a button after the click.
    By ramsrocker in forum New To Java
    Replies: 10
    Last Post: 02-15-2009, 06:52 AM
  5. AWT - catching click button event
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 03-11-2008, 11:02 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •