编程语言
首页 > 编程语言> > java – jFileChooser.showOpenDialog()冻结应用程序..没有错误/异常..几件事情

java – jFileChooser.showOpenDialog()冻结应用程序..没有错误/异常..几件事情

作者:互联网

我在NetBeans 6.9.1中创建了一个Summarizer项目,因为我有一个“Browse”按钮,它应该在JFileChooser上打开一个打开的对话框.我看了看这里: very similar question on stackoverflow

我的问题是一样的,我尝试设置当前目录,该目录在stackoverflow上尝试了另一个类似的问题,但即使这样也无法在我的PC上运行.

我仍然无法弄清楚我的错误到底是什么.我认为事情没有在EDT上运行也是同样的错误.我使用netbeans,代码很大.我无法找到在哪里更改EDT的事情.所以我只发布它的相关部分.请看看并告诉我如何解决我的问题?

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


        jFileChooser1.setCurrentDirectory(new File("F:/BE-Project/Summarizer"));
        jFileChooser1.setDialogTitle("Open File");
        jFileChooser1.setFileSelectionMode(JFileChooser.FILES_ONLY);
        int returnVal = jFileChooser1.showOpenDialog(Summarizer.this);
        if (returnVal== JFileChooser.APPROVE_OPTION) {
            try {

                fin = jFileChooser1.getSelectedFile();
                fileContents = Files.readFromFile(fin,"ISO-8859-1");
                tAreafileContents.setText( fileContents );
                txtInputFile.setText( fin.getAbsolutePath() + " -- " + fin.getName());
                tAreafileContents.setCaretPosition(tAreafileContents.getDocument().getLength());
            }
             catch (Exception e) {
                 System.out.println(e);
             }

        }

        else System.out.println("there is some error");
    }                           

/* netbeans generated code */
 public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Summarizer().setVisible(true);
            }
        });
    }             

请告诉我是否需要任何其他代码部分,请帮忙.我现在正在挠头.

解决方法:

我建议您的问题是在EDT中从磁盘读取文件.

 //this should be in a worker thread
 fileContents = Files.readFromFile(fin,"ISO-8859-1");

 //this then gets dumped back on the EDT
 tAreafileContents.setText( fileContents );
 txtInputFile.setText( fin.getAbsolutePath() + " -- " + fin.getName());
 tAreafileContents.setCaretPosition(tAreafileContents.getDocument().getLength());

标签:java,swing,jfilechooser,showdialog
来源: https://codeday.me/bug/20190710/1420287.html