JProgressBar无法更新,找不到线索。

3
很棒的工作,现在我想知道为什么如果我在while循环中添加System.out.println指令,下面的进度在Gui中和cmd中都显示了?
while(progress < 99){ 
  System.out.println("into while of PBar Thread progress = "+progress); 
  if(progress != Path.operationProgress){ 
    operationProgressBar.setValue(progress); 
    progress = Path.operationProgress; 
    operationProgressBar.repaint(); } }

need some help around , i can't get the JProgressBar to update, i can't use SwingWorker, i have to solve this without it . the variable Path.operationProgress is a static variable from a "Path" class instance, and it's updated from another thread, so i think the PBar and Path instances are both executed in user's Threads and not in the EDT . here is the Code of the progress bar :

    import javax.swing.*;
    public class Pbar extends Thread {
      JProgressBar operationProgressBar;
      public Pbar(JProgressBar operationProgressBar) {
          this.operationProgressBar = operationProgressBar;
      }

      @Override
      public void run() {
          int progress = Path.operationProgress;
          while(progress < 99) {
              if(progress != Path.operationProgress) {
                  operationProgressBar.setValue(progress);
                  progress = Path.operationProgress;
                  operationProgressBar.repaint();
              }}}
     }

this is the action that launches the threads :

private javax.swing.JProgressBar operationProgressBar;
private javax.swing.JLabel pathImage;
private javax.swing.JButton simulatedAnnelingButton;

public class TSPGUI extends javax.swing.JFrame {

    TSPMG tspInstance;
    Path p, result;
    String filename = "";
    int neighborHood_Type = 1, i = 0;
    // ......Constructor Stuff and init()

private void simulatedAnnelingButtonActionPerformed(java.awt.event.ActionEvent evt)

{
Thread sa = new Thread(){ @Override public void run(){ result = p.SimulatedAnnealing(neighborHood_Type); String lastCostString = result.Cost() + ""; lastCostLabel.setText(lastCostString); }}; sa.start(); Pbar pb = new Pbar(operationProgressBar); pb.start(); } //Some other Stuff ... }


1
尽管您小心地尝试在后台线程中执行操作,但您的问题似乎是一个线程问题——或者正在更新的JProgressBar不是正在显示的那个。然而,如果没有更多的代码,很难说。如果上面的代码在后台线程上运行,您必须注意在EDT上调用进度条setValue(...),虽然这不能解决您的问题,但它可以防止一些间歇性异常的发生。我建议您向我们展示更多的代码,最好是一个sscce - Hovercraft Full Of Eels
请为代码块使用一致和合乎逻辑的缩进,每行代码后不需要空白行! - Andrew Thompson
此外,您应该努力实现Runnable而不是扩展Thread。另外,您是如何运行线程的?您在调用它的start()还是run()方法?... 您的问题引发了更多的问题,最好通过sscce来回答。我知道我听起来像一张破唱片,但如果写得好,这些东西确实非常有帮助。 - Hovercraft Full Of Eels
代码已更新,但不易读。请查看我的回答下方的评论。 - Hovercraft Full Of Eels
2个回答

5
如果您无法使用SwingWorker,则可以使用SwingUtilities.invokeLater,例如:

if (progress != Path.operationProgress) {
    final int progressCopy = progress; // Probably not final so copy is needed
    SwingUtilities.invokeLater(new Runnable() {
        @Override
        void run() {
            operationsProgressBar.setValue(progressCopy);
        }
    });
}

注意:在这个过程中,run 中使用的所有内容必须是 final 的,或者必须采取其他措施来访问变量。在这方面,此代码是象征性的。
你需要在事件分派线程之外对 Swing 组件进行操作,没有其他方法。

不错,但正如@HovercraftFullOfEels所说,这似乎并没有解决问题。 - The Eighth Ero
@TheEighthEro:你仍然有线程冲突,而且可能不是EDT,而是很可能是与后台线程冲突。我强烈建议您发布小的、可编译并可运行的代码,供我们使用 [sscce](http://sscce.org)。我还建议您不要对变量进行100%轮询,而是让可观察对象在每次更改时将结果推送给观察者们。 - Hovercraft Full Of Eels

0
我会使用PropertyChangeListener来允许您将退火进度值作为类的“绑定”属性。然后,任何观察者都可以在需要时跟踪此属性。例如:
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import javax.swing.*;
import javax.swing.event.SwingPropertyChangeSupport;

@SuppressWarnings("serial")
public class TspGui2 extends JPanel {
   private static final String ANNEALING_PROGRESS = "Annealing Progress";
   private JProgressBar progBar = new JProgressBar(0, 100);
   private JLabel valueLabel = new JLabel();
   private JButton beginAnnealingBtn = new JButton("Begin Annealing");
   private MyAnnealing myAnnealing = new MyAnnealing(this);

   public TspGui2() {
      beginAnnealingBtn.addActionListener(new ActionListener() {

         @Override
         public void actionPerformed(ActionEvent e) {
            beginAnnealing();
         }
      });
      myAnnealing.addPropertyChangeListener(new PropertyChangeListener() {

         @Override
         public void propertyChange(PropertyChangeEvent evt) {
            if (evt.getPropertyName().equals(MyAnnealing.ANNEALING)) {
               // be sure this is done on the EDT
               SwingUtilities.invokeLater(new Runnable() {
                  public void run() {
                     int annealedValue = myAnnealing.getAnnealedValue();
                     setValue(annealedValue);
                     if (annealedValue >= MyAnnealing.MAX_ANNEALED_VALUE) {
                        beginAnnealingBtn.setEnabled(true);
                     }
                  }
               });
            }
         }
      });
      progBar.setString(ANNEALING_PROGRESS);
      progBar.setStringPainted(true);

      JPanel northPanel = new JPanel(new GridLayout(1, 0));
      northPanel.add(beginAnnealingBtn);
      northPanel.add(valueLabel);

      setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
      add(northPanel);
      add(progBar);
   }

   public void setValue(int value) {
      valueLabel.setText("Value:" + value);
      progBar.setValue(value);
   }

   public void beginAnnealing() {
      beginAnnealingBtn.setEnabled(false);
      setValue(0);
      myAnnealing.reset();
      new Thread(new Runnable() {
         public void run() {
            myAnnealing.beginAnnealing();
         }
      }).start();
   }

   private static void createAndShowGui() {
      TspGui2 mainPanel = new TspGui2();

      JFrame frame = new JFrame("TspGui2");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(mainPanel);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

class MyAnnealing {
   public static final String ANNEALING = "Annealing";
   public  static final int MAX_ANNEALED_VALUE = 100;
   private SwingPropertyChangeSupport propChangeSupport = 
         new SwingPropertyChangeSupport(this);
   private TspGui2 gui;
   private int annealedValue;

   public MyAnnealing(TspGui2 gui) {
      this.gui = gui;
   }

   public void addPropertyChangeListener(
         PropertyChangeListener listener) {
      propChangeSupport.addPropertyChangeListener(listener);
   }

   public void removePropertyChangeListener(
         PropertyChangeListener listener) {
      propChangeSupport.removePropertyChangeListener(listener);
   }

   public void reset() {
      setAnnealedValue(0);
   }

   // simulate some long process...
   public void beginAnnealing() {
      long sleepDelay = 100;
      while (annealedValue < MAX_ANNEALED_VALUE) {
         setAnnealedValue(annealedValue + 1);
         try {
            Thread.sleep(sleepDelay);
         } catch (InterruptedException e) {
            e.printStackTrace();
         }
      }
   }

   public int getAnnealedValue() {
      return annealedValue;
   }

   private void setAnnealedValue(int value) {
      final int oldValue = this.annealedValue;
      this.annealedValue = value;
      propChangeSupport.firePropertyChange(ANNEALING, oldValue, annealedValue);
   }   
}

@TheEighthEro:你不能在评论中发布代码,因为它根本无法阅读。 - Hovercraft Full Of Eels
@TheEighthEro:考虑格式化新代码并澄清问题。如果您大大简化代码并实际创建并发布与我上面发布的sscce类似的内容,那么帮助解决您的问题将变得更加容易。长时间运行的退火过程可以使用简单的Thread.sleep(...)进行模拟。 - Hovercraft Full Of Eels

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接