Reputation: 85
I have a button to choose a file using JFileChooser
.
This is my 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 ... ");
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) {
}
}
When I clicked one of the files in the JFileChooser
, and then clicked OK
, it's successful to load everything that I need. But, in another case, when I clicked a file, but I decided to choose Cancel
, the file is still loading in my textArea
. How to solve this problem?
Upvotes: -1
Views: 683
Reputation: 85
Thanks for all of the help, this code now work, if I make my code like this
int returnVal = chooser.showOpenDialog(null);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File f = chooser.getCurrentDirectory();
File ff = chooser.getSelectedFile();//
mainFile = ff;
if (ff != null) { t
nameFile = ff.toString();
File namaFilenya = new File(nameFile);
try {
FileReader readFile = new FileReader(namaFilenya);
try {
txtFile.read(readFile, null);
txtPathMainFile.setText(nameFile);
} catch (IOException ex) {
System.out.println(ex);
}
} catch (FileNotFoundException ex) {
System.out.println(ex);
}
}
}
Thanks..
Upvotes: 0
Reputation: 1
You should check the value returned by the JFileChooser
. See example from my answer
int rv = chooser.showOpenDialog(null);
if (rv == JFileChooser.APPROVE_OPTION) {
File file= chooser.getSelectedFile();
Upvotes: 1
Reputation: 46881
Check the response of the JFileChooser.
sample code:
int returnVal = chooser.showOpenDialog(parent);
if(returnVal == JFileChooser.APPROVE_OPTION) {
System.out.println("You chose to open this file: " +
chooser.getSelectedFile().getName());
// rest your code goes here
}
You can check for CANCEL_OPTION as well.
For information about using JFileChooser, see How to Use File Choosers, a section in The Java Tutorial.
Upvotes: 4
Reputation: 644
You should check whether the file is selected or not. i.e you must specify some if/else condition in your code.
if(fileSelected){
//load in text Area
}else{
//nothing
}
there should be some boolean value returned by the instance of JFileChooser.
Upvotes: 0