BlackBerry Forums Support Community               

Closed Thread
 
LinkBack Thread Tools
Old 02-26-2007, 02:33 PM   #1 (permalink)
New Member
 
Join Date: Feb 2007
Model: 8830
Carrier: Sprint
Posts: 3
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default Progress Bar popup using GaugeField

Please Login to Remove!

I've seen a lot of posts on this, trying to find out how to do it myself. Thought I would post my solution, hoping that it helps somebody else out.

This code creates a popup screen that has a constantly updated progress bar, for when you would like the user to know that you are not just ignoring them, and that the application really is busy.

This could be modified easily to post a popup, similar to Status.show(), except that it would not be removed from the screen until explicitly removed.



Code:
/**
 * Defines ProgressBar object. Creates popup screen with title, and perpetually updating
 * progress gauge. Instantiate and run as thread to start progress update. Call
 * remove() method when finished to remove popup screen and shutdown thread.
 */
package mrm.util;

import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.GaugeField;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.container.DialogFieldManager;
import net.rim.device.api.ui.container.PopupScreen;

public class ProgressBar extends Thread {

	private int maximum, timeout;

	private boolean useful;

	private PopupScreen popup;

	private GaugeField gaugeField;

	/**
	 * Object constructor
	 * 
	 * @param title
	 *           Text to display on popup area
	 * @param maximum
	 *           Range / width of the gauge field of progress bar
	 * @param timeout
	 *           Milliseconds to pause between updates to progress bar
	 * @see GaugeField
	 * @see Thread
	 * @see PopupScreen
	 */

	public ProgressBar(String title, int maximum, int timeout) {
		this.maximum = maximum;
		this.timeout = timeout;

		DialogFieldManager manager = new DialogFieldManager();

		popup = new PopupScreen(manager);
		gaugeField = new GaugeField(null, 1, maximum, 1, GaugeField.NO_TEXT);

		manager.addCustomField(new LabelField(title));
		manager.addCustomField(gaugeField);
	}

	/**
	 * run() method for starting thread
	 */

	public void run() {
		useful = true;

		UiApplication.getUiApplication().invokeLater(new Runnable() {
			public void run() {
				UiApplication.getUiApplication().pushScreen(popup);
			}
		});

		int iterations = 0;

		while (useful) {
			try {
				Thread.sleep(timeout);
			} catch (Exception e) {
			}

			if (++iterations > maximum)
				iterations = 1;

			gaugeField.setValue(iterations);
		}

		if (popup.isDisplayed()) {
			UiApplication.getUiApplication().invokeLater(new Runnable() {
				public void run() {
					UiApplication.getUiApplication().popScreen(popup);
				}
			});
		}
	}

	/**
	 * Method to shutdown the thread and remove the popup screen
	 *  
	 */

	public synchronized void remove() {
		useful = false;
	}
}
Offline  
Closed Thread


Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On





Copyright 2004-2014 BlackBerryForums.com.
The names RIM and BlackBerry are registered Trademarks of BlackBerry Inc.