JFileChooser.showOpenDialog () freezes the application .. not an error / exception .. several things are required

I am making the Summarizer project in NetBeans 6.9.1 so that I have a Browse button that should open an open dialog on JFileChooser. I looked here: stack overflow

My problem is the same, I tried to install the current directory, which was tried on another similar question in stackoverflow, but even this does not work on my PC.

I still can't figure out what the heck is my mistake. I think the same error is that things do not start on EDT. I use netbeans, the code is huge. I cannot find where to make edt changes. Therefore, I will publish only the relevant part. Please take a look and tell me what I need to do 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 any other part of the code is needed and please help. I'm scratching my head right now.

+3
source share
2 answers

I would suggest that your problem is reading Filefrom 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());
+1
source

, JFileChooser? F: , , USB-? , ? netbeans , F: , F

import java.io.*;

public class FileSize 
{
    public static void main(String [] args)
    {
        //String fileName = "F:/BE-Project/Summarizer/someFile.txt");
        String fileName = "FileSize.java";
        long size = new File(fileName).length();
        System.out.println("size: " + size);
    }

}
0

All Articles