ProgressMonitor或ProgressBar用于监视多个xml文件上载

时间:2012-09-21 09:57:10

标签: java

我试图找到一种方法,如果可能,在读取大量xml文件时显示progressBar或progressMonitor,以便修改文件的电子表格而无需用户打开每个xml文件并修改内容可能看起来很复杂的用户,我已经完成并且它的工作正常现在我坚持向用户显示progressBar不同于让用户等待没有任何东西表明某些事情正在进行中,当上传许多文件时,它需要接近十秒或者更多。非常感谢您的光线,感谢您提前。

  public Vector<Vector<SpreadSheetCell>> returnDynamicTrees(int h,int w) 
      {
          //Display thread shows the JProgress Bar for this time consuming method  
          new Thread(new display()).start();

          long ready = System.currentTimeMillis();
          System.out.println("Taking time ?");
           Vector<Vector<SpreadSheetCell>> dynamicTrees = new Vector<Vector<SpreadSheetCell>>();

          Vector<String> columns= columnHeaders(0,validDocs.size(),rootName);


         //Data reading here. 
         //This where too much delay is happening



         //I thought of putting this for loop in a thread 
         for(int j=0;j<fileNames.length;j++)
         {               
            dynamicTrees.add(new Vector<SpreadSheetCell>());
            for(int i=0;i<columns.size();i++)
            {
                dynamicTrees.elementAt(j).add(new SpreadSheetCell(directoryPath+"/"+fileNames[j],columns.get(i),i,j,h,w));

            }  
         }


             long done= System.currentTimeMillis();
          System.out.println(set);          
         System.out.println("Execution time for ReturnDynamicTree "+(done-ready)+" ms.");
         return dynamicTrees;

      }

       //////Method for display JProgressBar
       public static void displayProgressBar()
       {
        frame.setSize(300, 100); //Window size 300x100 pixels
        pane = frame.getContentPane();//Pane is a Container
        pane.add(bar);
        bar.setBounds(20, 20, 20, 20);  //Bar is JProgressBar     
        frame.setResizable(false);
        frame.setVisible(true);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);

        new Thread(new Repaint()).start();

       }


       static class Repaint implements Runnable{
         public void run(){
            for (int i=0; i<=100; i++){ //Progressively increment variable i
               bar.setValue(i); //Set value
               bar.repaint(); //Refresh graphics

               try{Thread.sleep(10);} //Sleep 50 milliseconds

                  catch (InterruptedException err){}
               System.out.println();             
            }

         }
      }
       //Thread for running displayProgressBar in a thread to avoid more delay
      static class display implements Runnable{
          public void run(){
            displayProgressBar();
            frame.repaint();
            }
         }

2 个答案:

答案 0 :(得分:0)

使用JProgressBar

progressBar = new JProgressBar(0, numberOfFiles);

在每个文件上传完成后执行progressBar.setValue

假设您正在逐个上传。

答案 1 :(得分:0)

使用javax.swing.ProgressMonitorInputStream。使用FilterInputStream封装JProgressBar。