带确认对话框的JFileChooser

我正在加载和保存来自文本文件的数据的程序,我要求用户在加载和保存JFileChooser文件的名称。

这个问题是关于保存对话框: new JFileChooser().showSaveDialog(); 。 用户然后可以覆盖现有的文件没有任何警告,这将是一个问题

有关如何解决这个问题的任何build议? 我一直在寻找一些方法或select,但我没有find任何东西。

提前致谢。

感谢您的答案,但我发现了另一个解决方法,覆盖JFileChooser的approveSelection(),这样:

 JFileChooser example = new JFileChooser(){ @Override public void approveSelection(){ File f = getSelectedFile(); if(f.exists() && getDialogType() == SAVE_DIALOG){ int result = JOptionPane.showConfirmDialog(this,"The file exists, overwrite?","Existing file",JOptionPane.YES_NO_CANCEL_OPTION); switch(result){ case JOptionPane.YES_OPTION: super.approveSelection(); return; case JOptionPane.NO_OPTION: return; case JOptionPane.CLOSED_OPTION: return; case JOptionPane.CANCEL_OPTION: cancelSelection(); return; } } super.approveSelection(); } } 

我希望这可能对别人有用。

正如AvrDragon所说的那样,用Xclosures不会被处理。 我添加了一个默认情况下处理所有不相关的选项:

 final JFileChooser fc = new JFileChooser() { private static final long serialVersionUID = 7919427933588163126L; public void approveSelection() { File f = getSelectedFile(); if (f.exists() && getDialogType() == SAVE_DIALOG) { int result = JOptionPane.showConfirmDialog(this, "The file exists, overwrite?", "Existing file", JOptionPane.YES_NO_CANCEL_OPTION); switch (result) { case JOptionPane.YES_OPTION: super.approveSelection(); return; case JOptionPane.CANCEL_OPTION: cancelSelection(); return; default: return; } } super.approveSelection(); } }; 

检查之前保存如果相同的文件已经存在,然后要求用户确认是否真的要覆盖:p

  JDialog.setDefaultLookAndFeelDecorated(true); int response = JOptionPane.showConfirmDialog(null, "Are you sure you want to override existing file?", "Confirm", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE); if (response == JOptionPane.NO_OPTION) { System.out.println("No button clicked"); } else if (response == JOptionPane.YES_OPTION) { System.out.println("Yes button clicked"); } else if (response == JOptionPane.CLOSED_OPTION) { System.out.println("JOptionPane closed"); } 

这里是代码

检查文件已经存在使用

 boolean exists = (new File("filename")).exists(); if (exists) { // File or directory exists } else { // File or directory does not exist } 

也许你可以validation文件不存在,甚至给JFileChooser一个FileSystemView (见这个构造函数 )

我根据你自己的回答写了这个。 张贴在其他人认为有用的情况下:

 final JFileChooser exportFileChooser = new JFileChooser(); exportFileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY); exportFileChooser.setApproveButtonText("Export"); final JButton exportButton = new JButton("Export text file"); exportButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { int returnVal = exportFileChooser.showSaveDialog(exportButton .getParent()); if (returnVal == JFileChooser.APPROVE_OPTION) { File outputFile = exportFileChooser.getSelectedFile(); if (outputFileIsValid(outputFile)) { exportFile(outputFile); } } } private boolean outputFileIsValid(File outputFile) { boolean fileIsValid = false; if (outputFile.exists()) { int result = JOptionPane.showConfirmDialog( exportButton.getParent(), "File exists, overwrite?", "File exists", JOptionPane.YES_NO_CANCEL_OPTION); switch (result) { case JOptionPane.YES_OPTION: fileIsValid = true; break; default: fileIsValid = false; } } else { fileIsValid = true; } return fileIsValid; } });