开发者

jFileChooser.showOpenDialog() freezes the application..no error / exception..tried several things

开发者 https://www.devze.com 2023-02-17 15:54 出处:网络
I am making a Summarizer project in NetBeans 6.9.1 in that I have a \"Browse\" button which should open a open dialog box on JFileChooser. I looked over here:very similar question on stackoverflow

I am making a Summarizer project in NetBeans 6.9.1 in that I have a "Browse" button which should open a open dialog box on JFileChooser. I looked over here: very similar question on stackoverflow

My开发者_如何学Go problem is just the same, I tried setting the current directory which was tried on another similar question on stackoverflow, but even that doesn't work on on my PC.

I still cant figure what the heck is my error. I think it is the same error that the things are not being run on EDT. I am using netbeans, the code is huge. I cant find where to make changes for the EDT thing. So I'll post only relevant part of it. Please see and tell me what do 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 code is needed, and please help. I am scratching my head right now.


I would suggest that your issue is in reading the File from disk in the 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());


Are you sure is a JFileChooser problem only? Is your F: unit a hard drive, a network share, a usb drive? If is not, could you try changing the unit to a hard drive? Run these test within netbeans and on command line, reading files on F: and some other unit different to 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

精彩评论

暂无评论...
验证码 换一张
取 消