EDT is not working properly with recursive method while updating lable

家住魔仙堡 提交于 2019-12-18 09:51:30

问题


//search files and directories

public  void getFile1(String directoryName) throws Exception {

    File directory = new File(directoryName);
    String str = directory.getName();
    File[] fList = directory.listFiles();
    if (fList != null) {
        for (File file : fList) {
            {                        

            if (file.isFile()) 
            {
                st = (file.toString());                 

               System.out.println(st);    
                jLable1.setText(st);//jLable1 is not updating.


                String fileName = file.getName();
                String strvirusCount=Integer.toString(virusCount); 
               }
            else if (file.isDirectory())

                    {
                      getFile1(file.getAbsolutePath());

                    }               

             globalCount++;       


    }

}

//while retrieving all files and folder unable to update label when process start to with retrieving path Swing form get unresponsive.


回答1:


You're executing a task that takes a long time (iterating through every file, recursively), from the event dispatch thread. So, while this method is executing, the EDT can't do anything else. So it can't do what it's supposed to do: repaint the components and react to user events.

This task should be done in a separate thread, using for example a SwingWorker, which will notify its progress regularly in order to update the label text. The javadoc has examples.



来源:https://stackoverflow.com/questions/22423696/edt-is-not-working-properly-with-recursive-method-while-updating-lable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!