Using the SWT ProgressBar Class

Progress bars are a great way to provide the user with feedback during a long-running process. As a general rule, users don’t like clicking a button to begin a process without some visual cue that the process has begun and is continuing. For short processes—those whose time to completion is measured in seconds—displaying an hourglass cursor is sufficient. However, for processes that are measured in tens of seconds, or minutes, a progress bar is required.

The SWT encapsulates all the functionality required to develop progress bars in a single class—ProgressBar . ProgressBar operates much like Slider, as can be demonstrated with a simple example, creating the window shown in Figure 16-3.

A ProgressBar example

Figure 16-3. A ProgressBar example

How do I do that?

Example 16-2 creates a simple ProgressBar added directly to a Shell, with the progress hardcoded at 50% of completion, as shown in Figure 16-3.

Example 16-2. A ProgressBar example

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.*;
public class ProgressBarExample {
    Display d;
    Shell s;
    ProgressBarExample( )    {
         d = new Display( );
         s = new Shell(d);
        s.setSize(250,250);
        s.setImage(new Image(d, "c:\\icons\\JavaCup.ico"));
        s.setText("A ProgressBar Example");

        final ProgressBar pb = new ProgressBar(s,SWT.HORIZONTAL); ...

Get SWT: A Developer's Notebook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.