【问题标题】:saving file using JFileChooser at any location在任何位置使用 JFileChooser 保存文件
【发布时间】:2015-05-02 03:15:43
【问题描述】:

您好,我正在尝试将文件保存在 Jfilechooser 指定的位置,现在的情况是当用户单击保存时,文件是在项目方向上创建的,名称为标题,但我需要为用户提供保存位置的选项文件并使用 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){

            } 

         }

         }

【问题讨论】:

  • 你可能想要fc.setSelectedFile(myFile); int returnVal = fc.showSaveDialog(buttonPressed); myFile = fc.getSelectedFile();之类的东西
  • @GuillaumePolet 不,这不起作用
  • 然后提供一个mcve,因为在这里它工作得很好。

标签: java swing jfilechooser


【解决方案1】:

试试

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-02-01
    • 1970-01-01
    • 2022-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多