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) {
}
}
Когда я щелкнул один из файлов в JFileChooser, а затем нажал «ОК», все, что мне нужно, удалось загрузить. Но в другом случае, когда я щелкнул файл, но решил выбрать «Отмена», файл все еще загружается в мою текстовую область. Как решить эту проблему?
У меня есть кнопка для выбора файла с помощью JFileChooser. Это мой код: [code]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 ... ");
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 (Exception e) { } } [/code] Когда я щелкнул один из файлов в JFileChooser, а затем нажал «ОК», все, что мне нужно, удалось загрузить. Но в другом случае, когда я щелкнул файл, но решил выбрать «Отмена», файл все еще загружается в мою текстовую область. Как решить эту проблему?