JProgressBar doesn't, well, progress...

I am trying to make a progress bar. RIght now, all it does is say 0% complete for the length of the task, and then jumps to 100%. Can anyone tell me what is wrong with this?

import java.awt.*;

import java.awt.event.*;

import java.io.*;

import java.util.*;

import javax.swing.*;

import javax.swing.event.*;

import java.beans.*;

import javax.swing.SwingWorker.*;

publicclass RunMethodextends SwingWorker<Void, Void>implements PropertyChangeListener{

public JProgressBar progressBar;

public Void doInBackground(){

setProgress(0);

returnnull;

}

publicvoid ProgressBar(String filenamed, String file2named)throws IOException{

JFrame pFrame =new JFrame();

pFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

pFrame.setLayout(new BorderLayout());

progressBar =new JProgressBar(0, 100);

progressBar.setValue(0);

progressBar.setStringPainted(true);

JPanel panel =new JPanel();

panel.add(progressBar);

pFrame.add(panel, BorderLayout.CENTER);

pFrame.setLocationRelativeTo(null);

pFrame.setSize(200,100);

pFrame.setVisible(true);

//setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));

RunMethod runMethod =new RunMethod();

runMethod.addPropertyChangeListener(this);

runMethod.runMethod(filenamed, file2named);

}

publicvoid runMethod(String filename, String file2name)throws IOException{

int progress = 0;

setProgress(progress);

//There's a bunch of code here, don't need to include it

int progress = 5;

setProgress(progress);

//And I have a bunch of setProgress's throughout, all the way to 100

}

publicvoid propertyChange(PropertyChangeEvent evt){

if ("progress" == evt.getPropertyName()){

System.out.println("here");

int progress = (Integer) evt.getNewValue();

progressBar.setValue(progress);

}

}

I have determined that the propertyChange method doesn't get called until after the task is complete.

don't rule out me doing something really obvious wrong, I'm new...

Thanks for the help

[3878 byte] By [snoboardera] at [2007-11-27 5:58:20]
# 1

caveat: this is a SWAG

It sounds like your program is running as if you didn't have a background thread at all. Could there be a problem with your having a GUI component (your jprogressbar) within your swingworker class? In the sun tutorial example, the component and the swingworker are kept separate. I wonder if this is keeping all of your background stuff actually running on the GUI thread. Again, this is just a silly wild-A guess. I'm just a neophyte in Swing and threads.

Message was edited by:

petes1234

petes1234a at 2007-7-12 16:32:46 > top of Java-index,Java Essentials,New To Java...
# 2
Post a small demo code that is generally compilable, runnable and could reproduce your problem. See: http://homepage1.nifty.com/algafield/sscce.html and http://www.yoda.arachsys.com/java/newsgroups.html
hiwaa at 2007-7-12 16:32:46 > top of Java-index,Java Essentials,New To Java...
# 3
huh, while I was doing that, I discovered my own problem, thanks
snoboardera at 2007-7-12 16:32:46 > top of Java-index,Java Essentials,New To Java...
# 4
> huh, while I was doing that, I discovered my own> problem, thanksand of course you are going to have the courtesy to give those who thought about your problem the benefit of your solution, right?
petes1234a at 2007-7-12 16:32:46 > top of Java-index,Java Essentials,New To Java...
# 5
haha, I'm still working on the solution, but somehow setProgress( int ) wasn't calling the Listener like halfway through my loop. Not sure exactly why it's doing that though...
snoboardera at 2007-7-12 16:32:46 > top of Java-index,Java Essentials,New To Java...
# 6
I'm having the same problem. Care to share what the problem is and how you solved it.
Kelly_Kellya at 2007-7-12 16:32:46 > top of Java-index,Java Essentials,New To Java...
# 7

of course, the code that works is here -- http://forum.java.sun.com/thread.jspa?threadID=5178584

My problem was that I was creating the progress bar inside the Task class. You need to create it outside Task, and use the progress bar to call Task.execute();

That should make it work

P.S. the entire class needs to be in the doInBackground() method in Task. Check out the tutorial

http://java.sun.com/docs/books/tutorial/uiswing/components/progress.html

Message was edited by:

snoboarder

snoboardera at 2007-7-12 16:32:46 > top of Java-index,Java Essentials,New To Java...