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 ... }
start()
还是run()
方法?... 您的问题引发了更多的问题,最好通过sscce来回答。我知道我听起来像一张破唱片,但如果写得好,这些东西确实非常有帮助。 - Hovercraft Full Of Eels