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
import totalcross.sys.Convert;
import totalcross.sys.Settings;
import totalcross.sys.Vm;
import totalcross.ui.Container;
import totalcross.ui.Control;
import totalcross.ui.MainWindow;
import totalcross.ui.ProgressBar;
import totalcross.ui.dialog.MessageBox;
import totalcross.ui.gfx.Color;
import totalcross.util.UnitsConverter;

public class ProgressBarSample extends MainWindow {
    ProgressBar  pbHYellow, pbVRed, pbVCyan, pbHRed, pbHPurple;
    int gap = UnitsConverter.toPixels(DP + 8);

    public ProgressBarSample() {
        setUIStyle(Settings.MATERIAL_UI);
    }

    @Override
    public void initUI() {
        try {
            super.initUI();

            Container sc = new Container();
            sc.setInsets(gap, gap, gap, gap);
            add(sc, LEFT, TOP, FILL, FILL);

            pbHPurple = new ProgressBar();
            pbHPurple.max = 50;
            pbHPurple.highlight = true;
            pbHPurple.suffix = " of " + pbHPurple.max;
            pbHPurple.textColor = 0xAAAA;
            pbHPurple.drawText = true;
            sc.add(pbHPurple, LEFT, TOP, FILL, PREFERRED);

            // endless ProgressBarSample
            pbHYellow = new ProgressBar();
            pbHYellow.max = width / 4; // max-min = width of the bar
            pbHYellow.setBackColor(Color.YELLOW);
            pbHYellow.setForeColor(Color.ORANGE);
            pbHYellow.prefix = "Loading, please wait...";
            pbHYellow.drawText = true;
            sc.add(pbHYellow, LEFT, AFTER + gap, FILL, PREFERRED);

            pbHRed = new ProgressBar();
            pbHRed.max = 50;
            pbHRed.setEndless();
            pbHRed.setBackForeColors(Color.DARK, Color.RED);
            sc.add(pbHRed, LEFT, AFTER + gap, FILL, FONTSIZE + 50);

            final int max = Settings.onJavaSE ? 2000 : 200;
            // vertical ones
            pbVCyan = new ProgressBar();
            pbVCyan.vertical = true;
            pbVCyan.max = max;
            pbVCyan.textColor = Color.BLUE;
            pbVCyan.setBackColor(Color.CYAN);
            pbVCyan.setForeColor(Color.GREEN);
            sc.add(pbVCyan, RIGHT, AFTER + gap, PREFERRED, FILL);

            pbVRed = new ProgressBar();
            pbVRed.vertical = true;
            pbVRed.max = 50;
            pbVRed.setBackForeColors(Color.RED, Color.DARK);
            sc.add(pbVRed, BEFORE - gap, SAME, FONTSIZE + 50, SAME);

            onSwapFinished();
        } catch (Exception ee) {
            MessageBox.showException(ee, true);
        }
    }

    @Override
    public void onSwapFinished() {
        final int ini = Vm.getTimeStamp();
        repaintNow();
        // runs the bench test
        int max = pbVCyan.max;
        for (int i = max; --i >= 0;) {
            int v = pbHPurple.getValue();
            v = (v + 1) % (pbHPurple.max + 1);
            Control.enableUpdateScreen = false; // since each setValue below updates the screen, we disable it to let it paint all at once at the end
            pbHPurple.setValue(v);
            pbVCyan.setValue(i);
            pbHYellow.setValue(5); // increment value
            pbHRed.setValue(v);
            Control.enableUpdateScreen = true;
            pbVRed.setValue(v);
            if (Settings.onJavaSE) {
                Vm.sleep(20);
            }
        }
    }
}

Attributes

Methods

References

  • See the Java Docs for more information.

  • You can check the example contained in the SDK, in tc.samples.api.ui ProgressBarSample.

Last updated