在更新标签时,EDT无法通过递归方法正常工作 [英] EDT is not working properly with recursive method while updating lable

查看:133
本文介绍了在更新标签时,EDT无法通过递归方法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

//搜索文件和目录

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++;       


    }

}

//在检索所有文件和文件夹时无法更新标签
当进程开始时使用检索路径Swing表单无响应。

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

推荐答案

您正在执行一项需要很长时间的任务(递归遍历每个文件),来自事件派发线程。因此,在执行此方法时,EDT无法执行任何其他操作。所以它不能做它应该做的事情:重新绘制组件并对用户事件做出反应。

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.

这个任务应该在一个单独的线程中完成,例如使用 SwingWorker ,将定期通知其进度以更新标签文本。 javadoc有例子。

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.

这篇关于在更新标签时,EDT无法通过递归方法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆