【问题标题】:Button cancel JFileChooser still load file when clicked按钮取消 JFileChooser 单击时仍会加载文件
【发布时间】: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


    【解决方案1】:

    查看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 的信息,请参阅 Java 教程中的部分 How to Use File Choosers

    【讨论】:

    • chooser.showOpenDialog(parent); 很高兴将其更改为parent.. 其余部分很好,但那部分真的让我很兴奋。 :)
    • @AndrewThompson 这是一个示例代码,传递对话框的父级是一个好习惯。
    • “传递对话框的父级是个好习惯。” +1 我们同意。
    【解决方案2】:

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

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

    【讨论】:

    • 这里不是特别的东西。当然应该是文件。
    • 带文件的部分不是我回答的主要部分,这是毫无疑问的回答。
    【解决方案3】:

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

    if(fileSelected){
    
    //load in text Area
    }else{
    //nothing
    }
    

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

    【讨论】:

      【解决方案4】:

      感谢所有的帮助,如果我这样编写代码,这段代码现在可以工作了

      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);
      
                      }
      
                  }
      
              }
      

      谢谢..

      【讨论】:

        猜你喜欢
        • 2020-11-05
        • 2018-02-14
        • 2023-03-10
        • 1970-01-01
        • 2021-07-08
        • 1970-01-01
        • 1970-01-01
        • 2019-01-29
        • 2014-10-28
        相关资源
        最近更新 更多