我的JFileChooser
课程有问题。我使用下面的类(我写的)一个接一个地加载几个文件,它通常适用于2或3个文件(有时1个,有时6个,看起来是随机的,即使它一定不是)并且在某一点上,它冻结在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上运行我的程序,一切正常。您是否了解与此类/方法有关的平台相关问题?
答案 0 :(得分:0)
在下面的代码块中使用您的代码。
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
}
});
}