×

ProgressBar

The ProgressBar can be used to show the progress of tasks that can be expressed as a percentage (determinate) or operations whose extend is unknown (indeterminate). Progress bars can optionally have a title and they can be displayed with or without an icon. They can also receive a ProgressBarListener that can trigger a Cancel event.

An indeterminate ProgressBar without any title can be initialized by passing the boolean toggle variable, as the only argument to the constructor:

Display.init(this);
form = new Form("Progress Bar");
ProgressBar bar = new ProgressBar(true);
form.addComponent(bar);
form.show();

The value of the toggle boolean variable should be setto true for the indeterminate ProgressBar and false for the determinate ProgressBar

Figure 1. Indeterminate ProgressBar without title

The progress for a determinate ProgressBar is represented as a percentage, within the range from 0 to 100. When dynamically updating the Progress Bar, we need to call the revalidate method in order to refresh the Form. The following snippet updates a determinate ProgressBar from 0 to 100 in increments of 10, every 1 second, within a Thread:

Display.init(this);
form = new Form("Progress Bar");
bar = new ProgressBar("Downloading...", false);
form.addComponent(bar);
form.show();

new Thread () {
    int progress = 0;
    public void run() {
        while (progress <= 100) {
            try {
                bar.setProgress(progress);
                progress = progress + 10;
                sleep(1000);
                form.revalidate();
            } catch (InterruptedException e) {
                // to be implemented
            }
        }
    }
}.start();

Figure 2. Determinate ProgressBar with a title

In addition to a title, the ProgressBar can receive an icon. The icon is displayed before the ProgressBar on the leftmost side for Left to Right writing scripts and on the rightmost side for Right to Left languages. Creating the Image instance is the developer’s responsibility. In the snippet below an image of dimensions 22 x 22 pixels was used to construct an indeterminate and determinate ProgressBar with an icon:

Display.init(this);

form = new Form("Progress Bar");

try {
    Image image = Image.createImage("/categorybar_windows_m_light.png");
    ProgressBar indefIconBar = new ProgressBar("Indefinite with icon", image, false, true);
    form.addComponent(indefIconBar);
} catch (IOException e) {
    // to be implemented
}

try {
    Image image = Image.createImage("/categorybar_windows_m_light.png");
    ProgressBar defIconBar = new ProgressBar("Definite with icon", image, false, false);
    defIconBar.setProgress(20);
    form.addComponent(defIconBar);
} catch (IOException e) {
    // to be implemented
}

form.show();

Figure 3. Indeterminate and determinate ProgressBar with icon

Progress bars can be interactive and they can receive a tap that triggers a cancel event. Making a Progress bar interactive is done by setting the constructor’s third boolean argument to true. Interactive Progress bars display a Cancel icon at the end of the line. A ProgressBarListener is used to catch the cancel event:

Display.init(this);

form = new Form("Progress Bar");

cancelForm = new Form("Canceled!");
Label cancelLabel = new Label("Progress was Canceled");
cancelForm.addComponent(cancelLabel);
backCommand = new Command("back");
cancelForm.setBackCommand(backCommand);
cancelForm.addCommandListener(new ActionListener() {
    public void actionPerformed(ActionEvent event) {
        form.show();
    }
});

try {
    Image image = Image.createImage("/categorybar_windows_m_light.png");
    ProgressBar indefIconBar = new ProgressBar("Indefinite with cancel", image, true, true);
    indefIconBar.setProgressBarListener(new ProgressBarListener() {
        public void notifyProgressBarListener(ProgressBar bar) {
            cancelForm.show();
        }
    });
    form.addComponent(indefIconBar);
} catch (IOException e) {
    // to be implemented
}

try {
    Image image = Image.createImage("/categorybar_windows_m_light.png");
    ProgressBar defIconBar = new ProgressBar("Definite with cancel", image, true, false);
    defIconBar.setProgress(20);
    defIconBar.setProgressBarListener(new ProgressBarListener() {
        public void notifyProgressBarListener(ProgressBar bar) {
            cancelForm.show();
        }
    });
    form.addComponent(defIconBar);
} catch (IOException e) {
    // to be implemented
}


form.show();

Figure 4. Indeterminate and determinate ProgressBar with Cancel events


Last updated 29 July 2013

Back to top

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×