JFileChooser没有正确关闭

时间:2013-07-25 00:44:28

标签: java swing jfilechooser

我正在开发一个需要使用jFileChooser Swing窗口的项目。 单击“取消”或“打开”时,窗口不会关闭。 在StackOverflow上查看了一些帖子并查看了JFileChooser tutorialdocumentation之后,我对导致这个重复问题的原因感到茫然。

我正在使用NetBeans来使用其swing编辑器。我还在Eclipse中尝试了该程序作为测试并获得了相同的结果。

新更新:

以下是我对SSCCE版本代码的尝试,感谢您的耐心等待:

//package imi_test;

import java.io.*;
import java.io.IOException;
import javax.swing.*;


public class FileSelector extends JFrame {

    //private static BufferedWriter out;
    //private static FileInputStream in;
    private static String selPath;
    private int val;
    //private static UniqueReader2 ur;



    public FileSelector() throws IOException {

        initComponents();        
    }

    public static String getSelPath(){
        return selPath;
    }



      public static void writeNewPath(String path) throws IOException{
      //This would write the selected folder's new unique file path to a file
            System.out.println("New file path written");
        /*
        out = new BufferedWriter(new FileWriter("recentPaths.txt",true));  
        in = new FileInputStream("recentPaths.txt");
        ur = new UniqueReader(new InputStreamReader(in));
        ur.main(null);
        try{


            if(ur.linePres){

            }else{

                out.write(path);
                out.newLine();
                out.flush();
                out.close();

                FileChecker.setFilePath(path);
            }             
        }catch(Exception e){System.err.println(e);}
*/
    }
//*******************************************************
    //Netbeans auto gen GUI code starts here       

    @SuppressWarnings("unchecked")

    private void initComponents() {

        jDialog1 = new javax.swing.JDialog();
        jMenuItem1 = new javax.swing.JMenuItem();
        jFrame1 = new javax.swing.JFrame();

        folderSelector = new javax.swing.JFileChooser();

        javax.swing.GroupLayout jDialog1Layout = new javax.swing.GroupLayout(jDialog1.getContentPane());
        jDialog1.getContentPane().setLayout(jDialog1Layout);
        jDialog1Layout.setHorizontalGroup(
            jDialog1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        jDialog1Layout.setVerticalGroup(
            jDialog1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );

        jMenuItem1.setText("jMenuItem1");

        javax.swing.GroupLayout jFrame1Layout = new javax.swing.GroupLayout(jFrame1.getContentPane());
        jFrame1.getContentPane().setLayout(jFrame1Layout);
        jFrame1Layout.setHorizontalGroup(
            jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        jFrame1Layout.setVerticalGroup(
            jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        folderSelector.setCurrentDirectory(new java.io.File("Computer"));
        folderSelector.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        folderSelector.setAcceptAllFileFilterUsed(false);
        folderSelector.setDialogTitle("Please Select a Folder");
        val = folderSelector.showOpenDialog(FileSelector.this);
        folderSelector.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                folderSelectorActionPerformed(evt);
            }
        });


        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(folderSelector, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(folderSelector, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        pack();
    }                        


    //Netbeans auto gen GUI code ends here
//*******************************************************

    private void folderSelectorActionPerformed(java.awt.event.ActionEvent evt) {                                               


        if (val== JFileChooser.APPROVE_OPTION) { 
             try{
                 selPath = folderSelector.getSelectedFile().getPath();
                 writeNewPath(selPath);
                 this.dispose();

             }catch(Exception ex){System.err.println(ex.getMessage() + "After folder selection");}
        }
        else{
           selPath = null;
           this.dispose();    
        }
    }                                              


    public static void main(String args[]) {

        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (Exception ex) {
            java.util.logging.Logger.getLogger(FileSelector.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }            

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                try{
                    new FileSelector().setVisible(true);
                }catch(Exception e){System.err.println(e.getMessage());}
            }
        });
    }
    // Variables declaration - do not modify                     
    private javax.swing.JFileChooser folderSelector;
    private javax.swing.JDialog jDialog1;
    private javax.swing.JFrame jFrame1;
    private javax.swing.JMenuItem jMenuItem1;
    // End of variables declaration                   
}

感谢您抽出宝贵时间并帮助解决此问题。

解: 从netbeans放入的文件中清除额外的东西,jDialog,JFrame和jMenuItem后,它按照预期的方式工作。

2 个答案:

答案 0 :(得分:2)

我认为您不应该向ActionListener添加JFileChooser,这很可能(根据您的代码)每次尝试关闭时都会打开一个新对话框。

相反,您应该直接打开对话框..

folderSelector = new JFileChooser(file);
folderSelector.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
folderSelector.setAcceptAllFileFilterUsed(false);
folderSelector.setDialogTitle("Please Select a Folder");

int val = folderSelector.showOpenDialog(folderSelector.this);
if (val== JFileChooser.APPROVE_OPTION) { 
    selPath = folderSelector.getSelectedFile().getPath();
}

这将显示一个对话框,这将导致程序执行暂停,直到对话框关闭,然后您可以验证选择了什么

答案 1 :(得分:1)

这里的主要问题是文件选择器被添加到框架!我的“当天学习项目”甚至可能

import java.io.*;
import java.io.IOException;
import javax.swing.*;

public class FileSelector extends JFrame {

    private static String selPath;
    private int val;

    public FileSelector() throws IOException {

        initComponents();
    }

    public static String getSelPath(){
        return selPath;
    }

    public static void writeNewPath(String path) throws IOException{
        //This would write the selected folder's new unique file path to a file
        System.out.println("New file path written");
    }

    private void initComponents() {

        jDialog1 = new javax.swing.JDialog();
        jMenuItem1 = new javax.swing.JMenuItem();
        jFrame1 = new javax.swing.JFrame();

        folderSelector = new javax.swing.JFileChooser();

        javax.swing.GroupLayout jDialog1Layout = new javax.swing.GroupLayout(jDialog1.getContentPane());
        jDialog1.getContentPane().setLayout(jDialog1Layout);
        jDialog1Layout.setHorizontalGroup(
            jDialog1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        jDialog1Layout.setVerticalGroup(
            jDialog1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );

        jMenuItem1.setText("jMenuItem1");

        javax.swing.GroupLayout jFrame1Layout = new javax.swing.GroupLayout(jFrame1.getContentPane());
        jFrame1.getContentPane().setLayout(jFrame1Layout);
        jFrame1Layout.setHorizontalGroup(
            jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        jFrame1Layout.setVerticalGroup(
            jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        folderSelector.setCurrentDirectory(new java.io.File("Computer"));
        folderSelector.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        folderSelector.setAcceptAllFileFilterUsed(false);
        folderSelector.setDialogTitle("Please Select a Folder");
        val = folderSelector.showOpenDialog(FileSelector.this);

        pack();
    }

    public static void main(String args[]) {
        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                try{
                    new FileSelector().setVisible(true);
                }catch(Exception e){System.err.println(e.getMessage());}
            }
        });
    }

    private javax.swing.JFileChooser folderSelector;
    private javax.swing.JDialog jDialog1;
    private javax.swing.JFrame jFrame1;
    private javax.swing.JMenuItem jMenuItem1;
}