jprogressbar - depends on method...

I was looking for jprogressbar, but I didn't find anything useful for me.

I have a method:

privatevoid SaveCurrentNetshButtonActionPerformed(java.awt.event.ActionEvent evt){

fileChooser.setApproveButtonText("save netsh conf");

if(JFileChooser.APPROVE_OPTION == fileChooser.showSaveDialog(this)){//if file was saved

ProgressBarCommon.setIndeterminate(true);

new Thread(){

publicvoid run(){

System.out.println("BAR IS LAUNCHED");

EditNetsh.saveNetshDumptoFile(fileChooser.getSelectedFile().getPath());

}

}.start();

ProgressBarCommon.setIndeterminate(false);

ProgressBarCommon.setValue(0);

//this.pack();

}

}

this method sends to command line :netsh dump interface > userFileNameAndPath.userExtension

It dumps network configuration to file.

This operation takes some time.

This static method performs such operation.

EditNetsh.saveNetshDumptoFile(fileChooser.getSelectedFile().getPath());

i want jProgressBar to be animated during dump operation (ProgressBarCommon.setIndeterminate(true);)

then I want it to stop, but nothing happens....

Where is mistake?

P.S.

I mean I get no error, I can save file, but ProgressBar is not animated....

Message was edited by:

Holod

[1983 byte] By [Holoda] at [2007-11-27 7:31:49]
# 1

I had a similar problem recently. I think its down to the fact that the file operation takes a higher thread priority than the JProgressBar. So the system waits for that to finish before updating the progress bar.

My solution was to use swingworker:

public boolean executeCMoveBackgroundThread(){

CMoveStatus = false;

CMoveComplete = false;

class CMoveThread extends javax.swing.SwingWorker<Boolean, Object> {

@Override

public Boolean doInBackground() {

return DoCMOVE(); //call function to do all the work

}

@Override

protected void done() {

try {

CMoveStatus = get();

CMoveComplete = true;

} catch (Exception ignore) {

}

}

}

CMoveThread cmovethread = new CMoveThread();

cmovethread.execute();

return CMoveStatus;

}

seabhcana at 2007-7-12 19:12:04 > top of Java-index,Desktop,Core GUI APIs...
# 2

>operation takes a higher thread priority than the JProgressBar

Hi;

It is not priority of threads. There is thread called Event Dispatch Thread (EDT), which must be used to run only GUI work.

For any other ordinary work we must use ordinary threads.

Read http://java.sun.com/products/jfc/tsc/articles/threads/threads1.html it gives full explanation for EDT.

As @seabhcan used SwingWorker it is specially designed for background work purpose.

GsrCa at 2007-7-12 19:12:04 > top of Java-index,Desktop,Core GUI APIs...
# 3

I think what you need is the following:

if(JFileChooser.APPROVE_OPTION == fileChooser.showSaveDialog(this))

{

Runnable save = new Runnable()

{

public void run()

{

System.out.println("BAR IS LAUNCHED");

EditNetsh.saveNetshDumptoFile(fileChooser.getSelectedFile().getPath());

//reset the progressBar after the thread is finished

ProgressBarCommon.setIndeterminate(false);

ProgressBarCommon.setValue(0);

}

}

ProgressBarCommon.setIndeterminate(true);

Thread saveThread = new saveThread(save);

saveThread.start();

}

Your progressBar is not animated because you set it back to determinate just after your thread starts. The Thread method does not block the execution of the follow up calls like a method call ....

marco@dea at 2007-7-12 19:12:04 > top of Java-index,Desktop,Core GUI APIs...
# 4

> I was looking for jprogressbar, but I didn't find anything useful for me.

Working example found in the Swing tutorial on [url http://java.sun.com/docs/books/tutorial/uiswing/components/progress.html]How to Use Progress Bars[/url].

The JProgressBar API also has a link to the tutorial so I'm not sure how you couldn' find anything usefull.

camickra at 2007-7-12 19:12:04 > top of Java-index,Desktop,Core GUI APIs...
# 5

I've launched in NetBeans all examples.

I've understood how jProgressbar is to be used, but i can't undersatand why my code is not working.....

As you can see in my method, when user gets fileSave dialog jprogressbar should become Indeterminate, but i doesn't become...

So i can;t understand why it is so....

Holoda at 2007-7-12 19:12:04 > top of Java-index,Desktop,Core GUI APIs...