0

You know, I have 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 file in chooser and then click OK, it success to load everything that I need. But, in another case when I clicked file but I decided to choose cancel, the file is still load in my textArea. How to solve this problem ?

Roman C
  • 49,761
  • 33
  • 66
  • 176
user2971238
  • 95
  • 1
  • 2
  • 9

4 Answers4

4

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.

Braj
  • 46,415
  • 5
  • 60
  • 76
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();
Roman C
  • 49,761
  • 33
  • 66
  • 176
0

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.

vermaraj
  • 634
  • 3
  • 10
  • 34
0

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..

user2971238
  • 95
  • 1
  • 2
  • 9