我正在加載和保存來自文本文件的數據的程序,我在加載並保存時詢問用戶使用JFileChooser的文件名。帶確認對話框的JFileChooser
這個問題是關於保存對話框:new JFileChooser().showSaveDialog();
。然後用戶可以在沒有任何警告的情況下覆蓋現有文件,並且將成爲問題。
有關如何解決此問題的任何建議?我一直在尋找一些方法或選項,但我沒有找到任何東西。
在此先感謝。
我正在加載和保存來自文本文件的數據的程序,我在加載並保存時詢問用戶使用JFileChooser的文件名。帶確認對話框的JFileChooser
這個問題是關於保存對話框:new JFileChooser().showSaveDialog();
。然後用戶可以在沒有任何警告的情況下覆蓋現有文件,並且將成爲問題。
有關如何解決此問題的任何建議?我一直在尋找一些方法或選項,但我沒有找到任何東西。
在此先感謝。
感謝您的答案,但我發現了另一個解決方案,覆蓋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();
}
}
我希望這可能是爲別人有用。
檢查保存,如果相同的文件已經存在,則要求用戶進行確認之前,難道她真的要改寫:對
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");
}
here是代碼
要檢查文件已經存在使用
boolean exists = (new File("filename")).exists();
if (exists) {
// File or directory exists
} else {
// File or directory does not exist
}
也許你可以驗證該文件不存在,甚至可以給JFileChooser一個FileSystemView
(請參閱this constructor)
我是根據你自己的回答寫的。張貼在情況下別人發現它有用:
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;
}
});
正如AvrDragon說,與X關閉不被處理。我添加了一個默認情況下處理所有不相關的選項:
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();
}
};
幫我一把!我做了同樣的事情,但是我在用戶選擇一個選項後無法讓對話框關閉。 「super.approveSelection()」幫助我。謝謝 – prolink007 2011-09-05 23:24:10
對於showSaveDialog,您應該處理選定文件的擴展名。 – onurbaysan 2016-03-06 11:28:36