Pages

2009-11-04

Indeterminate progress bar as popup window

A small indeterminate progress bar. Use like this:

pleaseWait wait = new pleaseWait();
wait.start();

/* some long operation */

wait.stop();

--- cut here –--

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Monitor;
import org.eclipse.swt.widgets.ProgressBar;
import org.eclipse.swt.widgets.Shell;

public class pleaseWait extends Thread {

        public void run() {
                Display display = new Display();
                Shell shell = new Shell(display, SWT.TITLE | SWT.BORDER | SWT.APPLICATION_MODAL);
                shell.setText("Please wait...");

                ProgressBar bar = new ProgressBar(shell, SWT.INDETERMINATE);
                bar.setBounds(1, 0, 200, 20);
                bar.setMaximum(5);
                bar.setMinimum(0);

                shell.pack();

                Monitor primary = display.getPrimaryMonitor();
                Rectangle bounds = primary.getBounds();
                Rectangle rect = shell.getBounds();

                int x = bounds.x + (bounds.width - rect.width) / 2;
                int y = bounds.y + (bounds.height - rect.height) / 2;

                shell.setLocation(x, y);
                shell.open();

                while (!shell.isDisposed()) {
                        if (!display.readAndDispatch())
                                display.sleep();
                }
        }
}
--- cut here –--



No comments:

Post a Comment