简体   繁体   中英

saving file using JFileChooser at any location

hello i am trying to save the file at location specified by Jfilechooser the situation now is when user click on save the file is created on the project direction with the name title but what i need is to give the user option where to save the file and choose the name he like using JFileChooser

if(buttonPressed.equals(save)){

         File myFile = new File("title");
         final JFileChooser fc = new JFileChooser();
         int returnVal = fc.showSaveDialog(null);
         fc.setSelectedFile(myFile);
         if (returnVal == JFileChooser.APPROVE_OPTION) {

            try (PrintWriter writer = new PrintWriter(myFile)) {   

            for (int i =0 ; i< cModel.getRowCount(); i++)
            {
               sData.add(cModel.getValueAt(i, 1).toString());
               eData.add(cModel.getValueAt(i, 2).toString());
               tData.add(cModel.getValueAt(i, 3).toString());
               writer.println(i+1 + "\n");
               writer.println(sData.get(i)+" --> " + eData.get(i));
               writer.println(tData.get(i));
               writer.println("\n");
            }
            }catch(FileNotFoundException ioEx){

            } 

         }

         }

Try

JFrame choose = new JFrame();
choose.setTitle("Save To ...");
final JFileChooser fc = new JFileChooser();
int returnVal = fc.showSaveDialog(choose);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM