Help needed on Basic JProgressBar

Hello all, this is really simple and basic, just need certain help on this.

I have a thread called MyThread, I want the my JProgressBar to show the progress of MyThread once a JButton is clicked.

Here is the code of MyThread :

publicclass MyThreadextends Thread{

publicvoid run(){

for(int i=0;i<2000;i++){

//loops 2000 times, prints out value

System.out.println(i);

}

}

}

as for the ActionPerformed method of the JButton :

publicvoid actionPerformed(ActionEvent e){

/*assume progressBar is a JProgressBar,

and it is added to a frame,

and its properties are all set to go

how do i allow the progressBar to monitor the progress

of thread1 ?*/

Thread thread1 =new MyThread();

}

my question is in the actionPerformed method...please help me out... my school is not going to teach me JProgressBars. I taught myself Threads. I am stuck at this point.

thanks

[1596 byte] By [TrAnScEnD3nTa] at [2007-11-26 22:47:13]
# 1

public class MyThread extends Thread{

JProgressBar jpb = new JProgressBar(0,2000);

public void run(){

for(int i=0;i<2000;i++){

jpb.setValue(i);

//loops 2000 times, prints out value

System.out.println(i);

}

}

}

Nethera at 2007-7-10 12:06:03 > top of Java-index,Desktop,Core GUI APIs...
# 2

> Hello all, this is really simple and basic, just need

> certain help on this.

There's more to it than you might think. You'll need to know the difference between a normal thread and the Event Dispatch Thread.

http://java.sun.com/docs/books/tutorial/uiswing/concurrency/index.html

http://java.sun.com/products/jfc/tsc/articles/threads/threads1.html

If your thread has to be in a different class file then you'll need a way for the JFrame to "listen" for progress in the thread, which is usually done by creating and implementing an interface.

http://java.sun.com/docs/books/tutorial/java/concepts/interface.html

Here's a quick example to get you started (without the interface and without a separete thread class file:

Thread thread = new Thread() {

public void run() {

for (int i = 0; i < 20; ++i) {

try {

final int value = i;

SwingUtilities.invokeAndWait(new Runnable() {

public void run() {

progBar.setValue(value);

}

});

Thread.sleep(100);

} catch (Exception e) {

e.printStackTrace();

}

}

}

};

thread.start();

Also note that it's customary to implement Runnable instead of extending Thread:

Runnable runnable = new Runnable() {

public void run() {

...

}

};

new Thread(runnable).start();

I trust that this is not part of the actual assignment and I'm not helping you cheat, otherwise you'd probably have had a better idea of what's involved at least. =)

BinaryDigita at 2007-7-10 12:06:03 > top of Java-index,Desktop,Core GUI APIs...
# 3

> public class MyThread extends Thread{

>

>JProgressBar jpb = new JProgressBar(0,2000);

> public void run(){

> for(int i=0;i<2000;i++){

>jpb.setValue(i);

> prints out value

> System.out.println(i);

> }

> }

> }

I'm sorry but that's bad advice, you should not update Swing components in any thread other than the EDT. You have to use SwingUtilities.invokeLater or SwingUtilities.invokeAndWait to set the progress bar's value. See the links I provided for the OP.

BinaryDigita at 2007-7-10 12:06:03 > top of Java-index,Desktop,Core GUI APIs...
# 4

> I'm sorry but that's bad advice, you should

> not update Swing components in any thread

> other than the EDT. You have to use

> SwingUtilities.invokeLater or

> SwingUtilities.invokeAndWait to set the progress

> bar's value. See the links I provided for the OP.

What exactly is wrong with setting the progressbar's value in the execution thread?

Nethera at 2007-7-10 12:06:03 > top of Java-index,Desktop,Core GUI APIs...
# 5

> What exactly is wrong with setting the progressbar's

> value in the execution thread?

My link to the OP says:

"Some Swing component methods are labelled "thread safe" in the API specification; these can be safely invoked from any thread. All other Swing component methods must be invoked from the event dispatch thread. Programs that ignore this rule may function correctly most of the time, but are subject to unpredictable errors that are difficult to reproduce."

http://java.sun.com/docs/books/tutorial/uiswing/concurrency/dispatch.html

The JProgressBar API says:

"Warning: Swing is not thread safe. For more information see Swing's Threading Policy."

http://java.sun.com/javase/6/docs/api/

Swing's Threading Policy says:

"In general Swing is not thread safe. All Swing components and related classes, unless otherwise documented, must be accessed on the event dispatching thread."

http://java.sun.com/javase/6/docs/api/javax/swing/package-summary.html#threading

BinaryDigita at 2007-7-10 12:06:03 > top of Java-index,Desktop,Core GUI APIs...
# 6

thank you all for replying .. it looks like knowledge only on threads only will not get me anywhere .. i'll go through all of the Sun's tutorials and pick it up... thanks

ps: BinaryDigit dont worry im picking this up myself due to passion for programming since my school is never going to teach such stuff =)

TrAnScEnD3nTa at 2007-7-10 12:06:03 > top of Java-index,Desktop,Core GUI APIs...