Java 为什么JProgressBar值不刷新?

Java 为什么JProgressBar值不刷新?,java,swing,jprogressbar,Java,Swing,Jprogressbar,我试图做的是,当我点击一个按钮,我从一个便携式驱动器(如usb)复制一些文件,并将这些文件复制到我的本地驱动器,然后我读取我以前复制的所有csv文件,并将其值放入arraylist并将其注入数据库,然后我可以删除这些文件,我想显示基于进程完成的进程进度条。我就是这么做的: void main() { JButton btnTransfer = new JButton("Transfer");

我试图做的是,当我点击一个按钮,我从一个便携式驱动器(如usb)复制一些文件,并将这些文件复制到我的本地驱动器,然后我读取我以前复制的所有csv文件,并将其值放入arraylist并将其注入数据库,然后我可以删除这些文件,我想显示基于进程完成的进程进度条。我就是这么做的:

           void main()
            {
             JButton btnTransfer = new JButton("Transfer");
                         Image transferIMG = ImageIO.read(new File("C:\\Users\\User\\Desktop\\images\\transfer.png"));
                         btnTransfer.setIcon(new ImageIcon(transferIMG));
                         btnTransfer.setPreferredSize(new Dimension(110, 90));
                         btnTransfer.setOpaque(false);
                         btnTransfer.setContentAreaFilled(false);
                         btnTransfer.setBorderPainted(false);
                         btnTransfer.setVerticalTextPosition(SwingConstants.BOTTOM);
                         btnTransfer.setHorizontalTextPosition(SwingConstants.CENTER);
                         btnTransfer.addActionListener(new ActionListener()
                         {
                              public void actionPerformed(ActionEvent e)
                              {
                                  File csvpath = new File(fileList1.getSelectedValue() + "\\salestablet\\report\\csv");
                                  File htmlpath = new File(fileList1.getSelectedValue() + "\\salestablet\\report\\html");
                                  String removepath = fileList1.getSelectedValue() + "\\salestablet\\report";

                                  if(csvpath.listFiles().length > 0 && htmlpath.listFiles().length > 0)
                                  {
                                       File[] csvarr = csvpath.listFiles();
                                       File[] htmlarr = htmlpath.listFiles();

                                       try 
                                       {
                                            copyFileUsingStream(csvarr, htmlarr, txt.getText(), removepath);
                                       } 
                                       catch (IOException e1) 
                                       {
                                           // TODO Auto-generated catch block
                                           e1.printStackTrace();
                                       }
                                 }
                              }
                         });

         JPanel ButtonCont = new JPanel(new GridLayout(4, 1, 5, 0));
                     ButtonCont.setBackground(Color.LIGHT_GRAY);

                     ButtonCont.add(btnTransfer);

         gui.add(ButtonCont , BorderLayout.EAST);

                     frame.setContentPane(gui);
                     frame.setExtendedState(Frame.MAXIMIZED_BOTH);  
                     frame.setMinimumSize(new Dimension(900, 100));
                     frame.pack();
                     frame.setLocationByPlatform(true);
                     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                     frame.setVisible(true);         
            }

      private static void copyFileUsingStream(File[] csvsources, File[] htmlsources, String dest, String removepath) throws IOException  
          {
               int count = 0;
               int MaxCount = countprocess(csvsources, htmlsources);

               progressBar =  new JProgressBar(0, MaxCount);
               progressBar.setStringPainted(true);

                InputStream is = null;
                OutputStream os = null;

                String csvfolderpath = dest + "\\csv";
                String htmlfolderpath = dest + "\\html";

                if(!(new File(csvfolderpath)).exists())
                {
                    (new File(csvfolderpath)).mkdirs(); //create csv folder;
                }

                if(!(new File(htmlfolderpath)).exists())
                {
                    (new File(htmlfolderpath)).mkdirs(); //create csv folder;
                }

                for(int i= 0; i < csvsources.length; i++) //copy all csv files to csv folder
                {
                    try 
                    {
                        is = new FileInputStream(csvsources[i]);
                        os = new FileOutputStream(csvfolderpath + "\\" + csvsources[i].getName());
                        byte[] buffer = new byte[1024];
                        int length;

                        while ((length = is.read(buffer)) > 0) 
                        {
                            os.write(buffer, 0, length);
                        }
                    }
                    finally
                    {
                        count += 1;
                        progressBar.setValue((count / MaxCount) * 100);
                        //progressBar.repaint();
                        //progressBar.revalidate();
                        progressBar.update(progressBar.getGraphics());

                        is.close();
                        os.close();
                    }
                }

                for(int i= 0; i < htmlsources.length; i++) //copy all html, images and css to html folder
                {
                    if(htmlsources[i].isFile())
                    {
                        try 
                        {
                            is = new FileInputStream(htmlsources[i]);
                            os = new FileOutputStream(htmlfolderpath + "\\" + htmlsources[i].getName());
                            byte[] buffer = new byte[1024];
                            int length;

                            while ((length = is.read(buffer)) > 0) 
                            {
                                os.write(buffer, 0, length);
                            }
                        }
                        finally
                        {
                            count += 1;
                            progressBar.setValue((count / MaxCount) * 100);
                            //progressBar.repaint();
                            //progressBar.revalidate();
                            progressBar.update(progressBar.getGraphics());

                            is.close();
                            os.close();
                        }
                    }
                    else if(htmlsources[i].isDirectory()) //for subfolder
                    {
                        String path = dest + "\\html\\" + htmlsources[i].getName();

                        if(!new File(path).exists())
                        {
                            (new File(path)).mkdirs(); //create subfolder;
                        }

                        File[] arr = (new File(htmlsources[i].getAbsolutePath())).listFiles();      

                        for(int j = 0; j < arr.length; j++)
                        {
                            if(arr[j].isFile())
                            {
                                try 
                                {
                                    is = new FileInputStream(arr[j]);
                                    os = new FileOutputStream(path + "\\" + arr[j].getName());
                                    byte[] buffer = new byte[1000000];
                                    int length;

                                    while ((length = is.read(buffer)) > 0) 
                                    {
                                        os.write(buffer, 0, length);
                                    }
                                }
                                finally
                                {
                                    if(htmlsources[i].getName().contains("images"))
                                    {
                                        count += 1;
                                        progressBar.setValue((count / MaxCount) * 100);
                                        //progressBar.repaint();
                                        //progressBar.revalidate();
                                        progressBar.update(progressBar.getGraphics());
                                    }

                                    is.close();
                                    os.close();
                                }
                            }
                        } 
                    }
                }

                ArrayList<String > DBValues = new ArrayList<String>(); //read all csv files values

                File f1 = new File(csvfolderpath); 
                for(int i = 0; i < f1.listFiles().length; i++)
                {
                     if(f1.listFiles()[i].isFile())
                     {
                        FileReader fl = new FileReader(f1.listFiles()[i]);
                        BufferedReader bfr = new BufferedReader(fl);    

                        for(int j = 0; j < 2; j++)
                        {
                            if(j == 1)
                            {
                                DBValues.add(bfr.readLine());

                                count += 1;
                                progressBar.setValue((count / MaxCount) * 100);
                                //progressBar.repaint();     
                                //progressBar.revalidate();
                                progressBar.update(progressBar.getGraphics());
                            }
                            else
                            {
                                bfr.readLine();
                            }
                        }
                        bfr.close();
                     }
                }

                /*for(int x = 0; x < DBValues.size(); x++)
                {
                    //System.out.println(DBValues.get(x));
                }*/

                //removing csv in local computer
                File f2 = new File(csvfolderpath); 
                File[] removelist = f2.listFiles();
                for(int x = 0; x < removelist.length; x++)
                {
                     if(removelist[x].isFile())
                     {
                         removelist[x].delete();

                         count += 1;
                         progressBar.setValue((count / MaxCount) * 100);
                        // progressBar.repaint();     
                         //progressBar.revalidate();
                            progressBar.update(progressBar.getGraphics());
                     }
                }

                //removing csv in device
                File f3 = new File(removepath + "\\csv");
                if(f3.isDirectory())
                {
                    removelist = f3.listFiles();

                   for(int y = 0; y < removelist.length; y++)
                   {
                       try
                       {
                           if(removelist[y].isFile())
                           {
                              //System.out.println(removelist[y].getName());
                              removelist[y].delete();
                              count += 1;
                              progressBar.setValue((count / MaxCount) * 100);
                              //progressBar.repaint();  
                             // progressBar.revalidate();
                            progressBar.update(progressBar.getGraphics());
                           }
                       }
                       catch(Exception e)
                       {
                          System.out.println(e);
                       }
                   }
                }

                //removing html and images in device
                File f4 = new File(removepath + "\\html");
                if(f4.isDirectory())
                {
                   removelist = f4.listFiles();

                   for(int z = 0; z < removelist.length; z++)
                   {
                       try
                       {
                           if(removelist[z].isFile())
                           {
                              removelist[z].delete();

                              count += 1;
                              progressBar.setValue((count / MaxCount) * 100);
                             // progressBar.repaint(); 
                             // progressBar.revalidate();
                            progressBar.update(progressBar.getGraphics());
                           }
                           else if(removelist[z].isDirectory())
                           {
                               if(removelist[z].getName().contains("images"))
                               {
                                   File[] subfolder = removelist[z].listFiles();

                                   for (int idx = 0; idx < subfolder.length; idx++)
                                   {
                                       if(subfolder[idx].isFile())
                                       {
                                          subfolder[idx].delete();

                                          count += 1;
                                          progressBar.setValue((count / MaxCount) * 100);
                                         // progressBar.repaint(); 
                                        //  progressBar.revalidate();
                                        progressBar.update(progressBar.getGraphics());
                                       }
                                   }
                               }
                           }
                       }
                       catch(Exception e)
                       {
                          System.out.println(e);
                       }
                   }
                }

                /*  JProgressBar progressBar = new JProgressBar();
                    progressBar.setValue(25);
                    progressBar.setStringPainted(true);*/
                    Border border = BorderFactory.createTitledBorder("Reading...");
                    progressBar.setBorder(border);


                gui.add(progressBar, BorderLayout.SOUTH);
                gui.repaint();
                gui.revalidate();


                // System.out.println(count);
            }

  private static int countprocess(File[] csv, File[] html_image)
  {
      int x = 0;
      int y = 0;
      int z = 0;

      for(int i = 0; i < csv.length; i++)
      {
          if(csv[i].isFile())
          {
              x += 1;
          }
      } //get total count of csv files throught loop

      for(int i = 0; i < html_image.length; i++)
      {
          if(html_image[i].isFile())
          {
              y += 1;
          }
          else if(html_image[i].isDirectory())
          {
              if(html_image[i].getName().contains("images"))
              {
                  File[] flist = html_image[i].listFiles();

                  for(int j = 0; j < flist.length; j++)
                  {
                      z += 1;
                  }
              }
          }  //get total count of html and images files throught loop
      }

      return ((4*x) + (2*y) + (2*z));
  }
void main()
{
JButton btnTransfer=新JButton(“传输”);
Image transferIMG=ImageIO.read(新文件(“C:\\Users\\User\\Desktop\\images\\transfer.png”);
btnTransfer.setIcon(新图像图标(transferIMG));
btnTransfer.setPreferredSize(新尺寸(110,90));
btnTransfer.setOpaque(false);
btnTransfer.setContentAreaFilled(false);
btnTransfer.com(假);
btnTransfer.setVerticalTextPosition(SwingConstants.BOTTOM);
btnTransfer.setHorizontalTextPosition(SwingConstants.CENTER);
btnTransfer.addActionListener(新的ActionListener()
{
已执行的公共无效操作(操作事件e)
{
File csvpath=新文件(fileList1.getSelectedValue()+“\\salestablet\\report\\csv”);
文件htmlpath=新文件(fileList1.getSelectedValue()+“\\salestablet\\report\\html”);
String removepath=fileList1.getSelectedValue()+“\\salestablet\\report”;
if(csvpath.listFiles().length>0&&htmlpath.listFiles().length>0)
{
文件[]csvarr=csvpath.listFiles();
文件[]htmlar=htmlpath.listFiles();
尝试
{
copyFileUsingStream(csvarr,htmlar,txt.getText(),removepath);
} 
捕获(IOE1异常)
{
//TODO自动生成的捕捉块
e1.printStackTrace();
}
}
}
});
JPanel ButtonCont=新的JPanel(新的网格布局(4,1,5,0));
按钮连接退刀台(颜色:浅灰色);
按钮CONT.add(btnTransfer);
添加(ButtonCont,BorderLayout.EAST);
frame.setContentPane(gui);
frame.setExtendedState(frame.MAXIMIZED_两者);
框架设置最小尺寸(新尺寸(900100));
frame.pack();
frame.setLocationByPlatform(真);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
私有静态void copyFileUsingStream(文件[]csvsources,文件[]htmlsources,字符串dest,字符串移除路径)引发IOException
{
整数计数=0;
int MaxCount=countprocess(csvsources、htmlsources);
progressBar=新的JProgressBar(0,最大计数);
progressBar.SetStringPaint(真);
InputStream=null;
OutputStream os=null;
字符串csvfolderpath=dest+“\\csv”;
字符串htmlfolderpath=dest+“\\html”;
如果(!(新文件(csvfolderpath)).exists())
{
(新文件(csvfolderpath)).mkdirs();//创建csv文件夹;
}
如果(!(新文件(htmlfolderpath)).exists())
{
(新文件(htmlfolderpath)).mkdirs();//创建csv文件夹;
}
for(int i=0;i0)
{
写操作(缓冲区,0,长度);
}
}
最后
{
计数+=1;
progressBar.setValue((计数/最大计数)*100);
//progressBar.repaint();
//progressBar.revalidate();
update(progressBar.getGraphics());
is.close();
os.close();
}
}
for(int i=0;i0)
{
写操作(缓冲区,0,长度);
}
}
fi
progressBar.setValue(count );
   new Thread()
  {
   public void run()
   {
       copyFileUsingStream(csvarr, htmlarr, txt.getText(), removepath);
  }
}.start();

} 
SwingUtilities.invokeLater(new Runnable() {

    @Override
    public void run() {

     count += 1;
     progressBar.setValue((int) (((double)count/ MaxCount) * 100));
    }
});