简体   繁体   English

更新jProgressBar的方式是什么?

[英]What is the ways updating jProgressBar?

I need to update jProgressBar in method which read from file and do some operations. 我需要更新从文件读取并执行一些操作的方法中的jProgressBar。 I tried to update progress bar by this method: 我试图通过这种方法更新进度条:

 public void progressUpdate(int percent) {
     System.out.println("Update = "+percent);
     synchronized (jMainProgressBar) {
         jMainProgressBar.setValue(percent);
     }
     SwingUtilities.invokeLater(
         new Runnable() {

             public void run() {
             jMainProgressBar.updateUI();
             jMainProgressBar.repaint();
             }
         });
 }

how ever this works only then when method is done. 只有在方法完成后,这才有效。 But if i continuously updating by this method then nothing happens. 但是,如果我通过这种方法不断更新,那么什么也不会发生。

Maybe some know how to improve this method? 也许有人知道如何改进这种方法?

It also would be nice for more suggestion Worker thread and else. 对于更多建议Worker线程以及其他情况,这也将很好。

You probably want to do 你可能想做

public void progressUpdate(final int percent) {
     SwingUtilities.invokeLater(new Runnable() {
         public void run() {
             jMainProgressBar.setValue(percent);
         }
     });
}

Based on the comments you provided (but not from the question!) you are performing heavy work on the Event Dispatch Thread (EDT). 根据您提供的评论(但不是来自问题!),您正在对事件调度线程(EDT)进行大量工作。 This blocks that thread and avoids any scheduled repaints to be performed. 这将阻塞该线程,并避免执行任何计划的重绘。 That is why you only see the update of your JProgressBar after the work is finished, as that is the moment the EDT becomes available to perform the repaint. 这就是为什么您仅在工作完成后看到JProgressBar的更新的原因,因为那是EDT可以执行重新绘制的时刻。

The solution is already provided in the links posted by others but it basically comes down to: 该解决方案已经在其他人发布的链接中提供,但基本上可以归结为:

  • perform the work on a worker thread 在工作线程上执行工作
  • update the progress on the JProgressBar on the EDT 在EDT上更新JProgressBar上的进度

The two most common ways to achieve this are using a SwingWorker or using SwingUtilities.invokeLater from the worker thread. 实现此目的的两种最常见方法是使用SwingWorker或使用工作线程中的SwingUtilities.invokeLater

All relevant links can be found in the answer of Yohan Weerasinghe 所有相关链接都可以在Yohan Weerasinghe的答案中找到

Check this out 看一下这个

 Timer barTimer;
 barTimer=new Timer(100,new ActionListener()
    {
     public void actionPerformed(ActionEvent e)
       {
        barvalue++;
        if(barvalue>jProgressBar1.getMaximum())
            {
                /* 
                 * To stop progress bar when it reaches 100 just write barTime.stop() 
                 */
               barTimer.stop();
               barvalue=0;

     }
    else
    {
        int a=(int)jProgressBar1.getPercentComplete();
        jProgressBar1.setStringPainted(true);
        jProgressBar1.setValue(barvalue);

      }
   }
    });
    barTimer.start();

Check the code at this link http://java23s.blogspot.in/2015/10/how-to-implement-progress-bar-in-java.html 在此链接中检查代码 http://java23s.blogspot.in/2015/10/how-to-implement-progress-bar-in-java.html

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

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