简体   繁体   English

Jdialog框中的Jprgressbar不更新

[英]Jprgressbar in Jdialog box not updating

i want to show the progress of copying the file from one folder to another using jprogressbar. 我想显示使用jprogressbar将文件从一个文件夹复制到另一个文件夹的进度。 All the thing i have done but jprogress bar run at the end and show 100% at the end. 我完成的所有工作,但jprogress栏最后运行,最后显示100%。 i am beginner and read topic in this form which says that i should use EDT, but still i didn't get all the thing. 我是初学者,并且以这种形式阅读主题,该主题表示我应该使用EDT,但仍然没有得到所有信息。 i also made another thread to update the Jprogress bar but nothing is happening. 我还做了另一个线程来更新Jprogress栏,但是什么也没有发生。 my part of code is 我的部分代码是

        jProgressBar1.setMinimum(0);
        jProgressBar1.setMaximum(100);
        try
        {
            fis = new FileInputStream(read);
            BufferedInputStream bins = new BufferedInputStream(fis);
            int b;
            long copied_data=0;
            for(int i =0;i<no_of_parts;i++)
            {
                copied_data = 0;
                fos = new FileOutputStream(jTextField2.getText()+"\\"+reading_file_name+".part"+i);
                bouts = new BufferedOutputStream(fos);
                while((b = bins.read())!= -1)
                {
                    bouts.write(b);
                    percentage = (progress*100)/file_size;
                    jProgressBar1.setValue(percentage);
                    copied_data++;
                    progress++;

                    if(copied_data==each_part_size_in_byte)
                    {
                        bouts.flush();
                        bouts.close();
                        break;
                    }
                }
                bouts.flush();
                bouts.close();

            }

        }catch(Exception e){}

The code that updates the progress bar should not be running on the same thread as the file copy operation. 更新进度条的代码不应与文件复制操作在同一线程上运行。 You need the file copy operation to run on a separate thread and a Swing timer to query the copy task for its progress and update the progress bar on the EDT periodically. 您需要文件复制操作在单独的线程上运行,并且需要Swing计时器来查询复制任务的进度,并定期更新EDT上的进度条。 You are currently performing everything on the EDT which prevents the progress bar from updating as you are locking up the event thread while the operation is in progress. 当前,您正在EDT上执行所有操作,这会阻止进度条在操作进行中锁定事件线程时进行更新。 You should only use the EDT for updating Swing components and everything else should be run in other threads. 您仅应使用EDT来更新Swing组件,而其他所有内容都应在其他线程中运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM