[英]JFileChooser.showOpenDialog() freezing my program
JFileChooser
類有問題。 我正在使用以下類(我確實編寫的)一個接一個地加載多個文件,它通常適用於 2 或 3 個文件(有時是 1,有時是 6,看起來是隨機的,即使它必須不是)並且在某一點,它在showOpenDialog(null),
處凍結showOpenDialog(null),
沒有拋出異常,也沒有返回任何內容。 我真的不知道它從哪里來。
這是我的課:
public class CustomFileChooser extends JFileChooser {
public File chooseFile(String windowTitle, String description, String extension, boolean mustExist) {
setDialogTitle(windowTitle);
resetChoosableFileFilters();
setAcceptAllFileFilterUsed(false);
addChoosableFileFilter(new CustomFileFilter(description, new String[] {extension}));
setSelectedFile(new File(""));
if (mustExist) {
setApproveButtonText("Open");
} else {
setApproveButtonText("Save");
}
File file = null;
while (file == null) {
if (showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
file = getSelectedFile();
if (mustExist) {
if (!file.canRead()) {
file = null;
JOptionPane.showMessageDialog(null, "Cannot read from the specified file!", "Error while opening the file", JOptionPane.ERROR_MESSAGE);
}
} else {
if (!file.getName().toLowerCase().endsWith(extension.toLowerCase())) {
file = new File(file.getAbsolutePath().concat(extension));
}
if (file.exists()) {
if (file.canWrite()) {
if (JOptionPane.showConfirmDialog(null, "Do you really want to overwrite this file?", "Erasing file", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE) == JOptionPane.NO_OPTION) {
file = null;
}
} else {
file = null;
JOptionPane.showMessageDialog(null, "Cannot write to the specified file!", "Error while opening the file", JOptionPane.ERROR_MESSAGE);
}
}
}
} else {
return null;
}
}
return file;
}
private static final long serialVersionUID = 1L;
}
編輯:我嘗試在 Windows 上運行我的程序,一切正常。 您是否了解有關此類/方法的平台相關問題?
在代碼塊中使用您的代碼,如下所示。
private void fileChooserMethod() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
javax.swing.JFileChooser fc_file_selector= new JFileChooser();
int response = fc_file_selector.showOpenDialog(null);
//your code here
}
});
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.