按钮取消JFileChooser在单击时仍然加载文件

时间:2014-07-19 14:13:10

标签: java swing filechooser

您知道,我有按钮可以使用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中加载。如何解决这个问题?

4 个答案:

答案 0 :(得分:4)

检查JFileChooser的响应。

示例代码:

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
}

您也可以查看CANCEL_OPTION

有关使用JFileChooser的信息,请参阅The Java Tutorial中的How to Use File Choosers部分。

答案 1 :(得分:1)

您应该检查JFileChooser返回的值。请参阅我的回答中的示例

int rv = chooser.showOpenDialog(null);
if (rv == JFileChooser.APPROVE_OPTION) {
  File file= chooser.getSelectedFile();

答案 2 :(得分:0)

您应该检查文件是否被选中。即你必须在你的代码中指定一些if / else条件。

if(fileSelected){

//load in text Area
}else{
//nothing
}

JFileChooser实例应该返回一些布尔值。

答案 3 :(得分:0)

感谢所有的帮助,如果我像这样编写代码

,这段代码现在可以使用了
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);

                }

            }

        }

谢谢..