【发布时间】:2014-07-19 14:13:09
【问题描述】:
你知道,我有使用JFileChooser 选择文件的按钮。这是我的代码
private void buttonFIleBrowserInPanelActionPerformed(java.awt.event.ActionEvent evt) {
try {
JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory(new java.io.File("."));
chooser.setDialogTitle("Please, choose your main file ... ");
chooser.setFileFilter(new FileFilter() {
@Override
public boolean accept(File f) {
return f.getName().toLowerCase().endsWith(".java") || f.isDirectory();
}
@Override
public String getDescription() {
return "Java File (*.java)";
}
});
chooser.setAcceptAllFileFilterUsed(true);
chooser.showOpenDialog(null);
File f = chooser.getCurrentDirectory();
File ff = chooser.getSelectedFile();
mainFile = ff;
if (ff != null) {
nameFile = ff.toString();
File namaFilenya = new File(nameFile);
try {
FileReader readFile = new FileReader(namaFilenya);
String readFileToString = readFile.toString();
try {
txtFile.read(readFile, null); // this is the textArea that show the contains of file
txtPathMainFile.setText(nameFile); //this is the textarea that show the path of file
} catch (IOException ex) {
System.out.println(ex);
}
} catch (FileNotFoundException ex) {
System.out.println(ex);
}
}
} catch (Exception e) {
}
}
当我单击选择器中的一个文件然后单击确定时,它成功加载了我需要的所有内容。但是,在另一种情况下,当我单击文件但我决定选择取消时,该文件仍然加载到我的 textArea 中。如何解决这个问题?
【问题讨论】:
标签: java swing filechooser