How use progress bar into applet?

L

Lauwie

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);
}
});
}

}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top