java – jFileChooser. Showopendialog() freezes the application No errors / exceptions A few things

I'm in NetBeans 6.9 1, because I have a "Browse" button, it should open an open dialog box on Jfilechooser Very similar question on stackoverflow

My problem is the same. I tried to set the current directory, which tried another similar problem on stackoverflow, but even then it can't run on my PC

I still can't figure out what my mistake is I think it's the same mistake that things don't run on EDT I use NetBeans and the code is very large I can't find out where to change EDT So I only publish the relevant parts of it Please look and tell me how to solve my problem?

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

Please tell me if I need any other code parts, please help I'm scratching my head now

Solution

I suggest that your problem is to read files from disk in 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());
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>