|
How to create a ProgressBar |
|
|
Progress bars are used with long tasks to show the user how much time is left before the
task is complete.
Progress bars are implemented in Swing using JProgressBar class. The example code below
shows how to use progress bars in Swing applications:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Progress extends JFrame {
JProgressBar current;
JTextArea out;
JButton find;
Thread runner;
int num = 0;
public Progress() {
super("Progress");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel pane = new JPanel();
pane.setLayout(new FlowLayout());
current = new JProgressBar(0, 2000);
current.setValue(0);
current.setStringPainted(true);
pane.add(current);
setContentPane(pane);
}
public void iterate() {
while (num < 2000) {
current.setValue(num);
try {
Thread.sleep(1000);
} catch (InterruptedException e) { }
num += 95;
}
}
public static void main(String[] arguments) {
Progress frame = new Progress();
frame.pack();
frame.setVisible(true);
frame.iterate();
}
}
|
Related Tips
|
Page 1 of 0 ( 0 comments )
You can share your information about this topic using the form below!
Please do not post your questions with this form! Thanks.