-
Notifications
You must be signed in to change notification settings - Fork 0
/
10c_progressbar_label.java
64 lines (50 loc) · 1.59 KB
/
10c_progressbar_label.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import javax.swing.*;
import java.awt.event.*;
public class GUIApp {
public static void main(String[] args) {
// https://docs.oracle.com/javase/7/docs/api/javax/swing/JSlider.html
JFrame frame = new JFrame("Cupertinii Swing GUI");
frame.setSize(500, 500);
// I had to comment following line out to get the slider
// frame.setLayout(null);
frame.setLayout(null);
frame.setVisible(true);
progressBarHorizontalExample(frame);
}
public static void progressBarHorizontalExample(JFrame frame) {
// JSlider mySlider = new JSlider(JSlider.HORIZONTAL);
int min = 0;
int max = 100;
int current_value = 0;
JProgressBar myProgressBar = new JProgressBar(0, 100);
myProgressBar.setLocation(20, 20);
myProgressBar.setSize(200, 60);
myProgressBar.setValue(44);
JLabel label = new JLabel();
label.setBounds(250, 25, 100, 50);
frame.add(label);
frame.add(myProgressBar);
// Call the function to increment the progress bar...
startProgress(myProgressBar, label);
}
public static void startProgress(JProgressBar myProgressBar, JLabel label) {
int i = 0;
while (i <= 100) {
// Set the Progress bar value
myProgressBar.setValue(i);
label.setText( String.valueOf(i) + "%");
// Wait for a half a second
Wait(500);
// Increment the count by 10. This will increase the progress bar by 10
i += 10;
}
}
public static void Wait(int milliseconds) {
try {
Thread.sleep(milliseconds);
}
catch(InterruptedException ex) {
System.out.println("Error"+ ex);
}
}
}