我的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上运行我的程序,并且一切正常。您是否了解与此类/方法有关的平台相关问题?
参考方案
在下面的代码块中使用您的代码。
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
}
});
}
java.awt.HeadlessException - javaJFileChooser chooser = new JFileChooser(); JDialog dialog=new JDialog(); dialog.setAlwaysOnTop(true); /*System.out.println("is always on top?"+dialog.isAlwaysOnTop());*/ …
Java检测文件系统中的更改 - java我有一个文件夹,其中不断有新文件被转储。在Java中,什么是检测文件系统更改的最佳方法(即,要在其中转储文件的指定文件夹)并将新到达的文件添加到队列数据结构,以便我可以顺序处理每个传入文件。我知道File类中的listFiles()函数,但是使用此函数,我只能获取瞬间可用的文件。当然,我可以连续轮询文件夹并使用线程获取其中的文件列表,但这是最好的方法还是有更…
java:继承 - java有哪些替代继承的方法? java大神给出的解决方案 有效的Java:偏重于继承而不是继承。 (这实际上也来自“四人帮”)。他提出的理由是,如果扩展类未明确设计为继承,则继承会引起很多不正常的副作用。例如,对super.someMethod()的任何调用都可以引导您通过未知代码的意外路径。取而代之的是,持有对本来应该扩展的类的引用,然后委托给它。这是与Eric…
Java:BigInteger,如何通过OutputStream编写它 - java我想将BigInteger写入文件。做这个的最好方式是什么。当然,我想从输入流中读取(使用程序,而不是人工)。我必须使用ObjectOutputStream还是有更好的方法?目的是使用尽可能少的字节。谢谢马丁 参考方案 Java序列化(ObjectOutputStream / ObjectInputStream)是将对象序列化为八位字节序列的一种通用方法。但…
Java-如何将此字符串转换为日期? - java我从服务器收到此消息,我不明白T和Z的含义,2012-08-24T09:59:59Z将此字符串转换为Date对象的正确SimpleDateFormat模式是什么? java大神给出的解决方案 这是ISO 8601标准。您可以使用SimpleDateFormat simpleFormat = new SimpleDateFormat("yyyy-MM…