Derek said:
Can you write me an example, please?.
And, I´m using Swing.
Here's a simple example, hope it helps:
public class ProgressApplet extends JApplet implements ActionListener
{
private JProgressBar progressBar;
public void init()
{
getContentPane().setBackground(Color.WHITE);
getContentPane().setLayout(new FlowLayout());
progressBar = new JProgressBar();
getContentPane().add(progressBar);
JButton cmd = new JButton("Do Task");
cmd.addActionListener(this);
getContentPane().add(cmd);
}
/**
* @see java.awt.event.ActionListener#actionPerformed(ActionEvent)
*/
public void actionPerformed(ActionEvent e)
{
new Worker().start();
}
private class Worker extends Thread
{
private void updateBar(Runnable runner)
{
try
{
SwingUtilities.invokeAndWait(runner);
}
catch (Exception e)
{
}
}
public void run()
{
progressBar.setValue(0);
progressBar.setMinimum(0);
progressBar.setMaximum(100);
//do your task here
for (int x = 0; x < 100; x++)
{
//sample task
try
{
Thread.sleep(10L);
}
catch (Exception e)
{
}
//
final int current = x;
updateBar(new Runnable()
{
public void run()
{
progressBar.setValue(current);
}
});
}
}
}
}