Search…
Progress Bar

Overview

It is a bar that can demonstrate the progress of a particular request or a loading of an event. You can have text that indicates the current status of the ProgressBar and can be used both horizontally and vertically.

Source code

ProgressBarSample
1
import totalcross.sys.Convert;
2
import totalcross.sys.Settings;
3
import totalcross.sys.Vm;
4
import totalcross.ui.Container;
5
import totalcross.ui.Control;
6
import totalcross.ui.MainWindow;
7
import totalcross.ui.ProgressBar;
8
import totalcross.ui.dialog.MessageBox;
9
import totalcross.ui.gfx.Color;
10
import totalcross.util.UnitsConverter;
11
12
public class ProgressBarSample extends MainWindow {
13
ProgressBar pbHYellow, pbVRed, pbVCyan, pbHRed, pbHPurple;
14
int gap = UnitsConverter.toPixels(DP + 8);
15
16
public ProgressBarSample() {
17
setUIStyle(Settings.MATERIAL_UI);
18
}
19
20
@Override
21
public void initUI() {
22
try {
23
super.initUI();
24
25
Container sc = new Container();
26
sc.setInsets(gap, gap, gap, gap);
27
add(sc, LEFT, TOP, FILL, FILL);
28
29
pbHPurple = new ProgressBar();
30
pbHPurple.max = 50;
31
pbHPurple.highlight = true;
32
pbHPurple.suffix = " of " + pbHPurple.max;
33
pbHPurple.textColor = 0xAAAA;
34
pbHPurple.drawText = true;
35
sc.add(pbHPurple, LEFT, TOP, FILL, PREFERRED);
36
37
// endless ProgressBarSample
38
pbHYellow = new ProgressBar();
39
pbHYellow.max = width / 4; // max-min = width of the bar
40
pbHYellow.setBackColor(Color.YELLOW);
41
pbHYellow.setForeColor(Color.ORANGE);
42
pbHYellow.prefix = "Loading, please wait...";
43
pbHYellow.drawText = true;
44
sc.add(pbHYellow, LEFT, AFTER + gap, FILL, PREFERRED);
45
46
pbHRed = new ProgressBar();
47
pbHRed.max = 50;
48
pbHRed.setEndless();
49
pbHRed.setBackForeColors(Color.DARK, Color.RED);
50
sc.add(pbHRed, LEFT, AFTER + gap, FILL, FONTSIZE + 50);
51
52
final int max = Settings.onJavaSE ? 2000 : 200;
53
// vertical ones
54
pbVCyan = new ProgressBar();
55
pbVCyan.vertical = true;
56
pbVCyan.max = max;
57
pbVCyan.textColor = Color.BLUE;
58
pbVCyan.setBackColor(Color.CYAN);
59
pbVCyan.setForeColor(Color.GREEN);
60
sc.add(pbVCyan, RIGHT, AFTER + gap, PREFERRED, FILL);
61
62
pbVRed = new ProgressBar();
63
pbVRed.vertical = true;
64
pbVRed.max = 50;
65
pbVRed.setBackForeColors(Color.RED, Color.DARK);
66
sc.add(pbVRed, BEFORE - gap, SAME, FONTSIZE + 50, SAME);
67
68
onSwapFinished();
69
} catch (Exception ee) {
70
MessageBox.showException(ee, true);
71
}
72
}
73
74
@Override
75
public void onSwapFinished() {
76
final int ini = Vm.getTimeStamp();
77
repaintNow();
78
// runs the bench test
79
int max = pbVCyan.max;
80
for (int i = max; --i >= 0;) {
81
int v = pbHPurple.getValue();
82
v = (v + 1) % (pbHPurple.max + 1);
83
Control.enableUpdateScreen = false; // since each setValue below updates the screen, we disable it to let it paint all at once at the end
84
pbHPurple.setValue(v);
85
pbVCyan.setValue(i);
86
pbHYellow.setValue(5); // increment value
87
pbHRed.setValue(v);
88
Control.enableUpdateScreen = true;
89
pbVRed.setValue(v);
90
if (Settings.onJavaSE) {
91
Vm.sleep(20);
92
}
93
}
94
}
95
}
Copied!

Attributes

Type
Name
Description
int
max
Progress Bar maximum value.
int
value
Current value of progress Bar.
String
prefix
It is the text that appears to the left of the value, remembering that the text output is prefix + value + sufix
String
suffix
It is the text that appears to the right of the value, remembering that the text output is prefix + value + suffix
boolean
drawText
It will indicate if the text will be displayed in progress bar or not, by default it comes as false.
boolean
drawValue
It will indicate if the value will be displayed in the progress bar.

Methods

Type
Name
Description
Constructor
ProgressBar()
Instances a ProgressBar with the minimum values 0 and maximum value 100.
Constructor
ProgressBar(int min, int max)
Instances a ProgressBar with the values passed in the variables min and max.
Void
setEndless()
Use in a horizontal ProgressBar to leave it without end.
Void
setValue(int n)
Updates the current progressbar value and draws the ProgressBar with the updated state.
Void
setValue(int value, String prefix, String suffix)
Updates the current value of progressBar and draws it again with the updated state and with texts before the value and after the value.

References

  • See the Java Docs for more information.
  • You can check the example contained in the SDK, in tc.samples.api.ui ProgressBarSample.