Java Tutorial/SWT/ProgressBar

Материал из Java эксперт
Перейти к: навигация, поиск

Introducing ProgressBar

Progress bars display incremental progress. ProgressBar Styles:

StyleDescriptionSWT.SMOOTHCreates a progress bar that displays a continuous bar as its indicator. The default is to display a distinctly divided bar.SWT.HORIZONTALCreates a horizontal progress bar.SWT.VERTICALCreates a vertical progress bar.SWT.INDETERMINATECreates a progress bar that constantly cycles, indicating continuous work.


Update a progress bar from another thread

   <source lang="java">

/*******************************************************************************

* Copyright (c) 2000, 2004 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
*     IBM Corporation - initial API and implementation
*******************************************************************************/

import org.eclipse.swt.SWT; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.ProgressBar; import org.eclipse.swt.widgets.Shell; public class ProgressBarUpdateAnotherThread {

 public static void main(String[] args) {
   final Display display = new Display();
   Shell shell = new Shell(display);
   final ProgressBar bar = new ProgressBar(shell, SWT.SMOOTH);
   bar.setBounds(10, 10, 200, 32);
   shell.open();
   final int maximum = bar.getMaximum();
   new Thread() {
     public void run() {
       for (final int[] i = new int[1]; i[0] <= maximum; i[0]++) {
         try {
           Thread.sleep(100);
         } catch (Throwable th) {
         }
         if (display.isDisposed())
           return;
         display.asyncExec(new Runnable() {
           public void run() {
             if (bar.isDisposed())
               return;
             bar.setSelection(i[0]);
           }
         });
       }
     }
   }.start();
   while (!shell.isDisposed()) {
     if (!display.readAndDispatch())
       display.sleep();
   }
   display.dispose();
 }

}</source>





Update a progress bar from the UI thread

   <source lang="java">

import org.eclipse.swt.SWT; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.ProgressBar; import org.eclipse.swt.widgets.Shell; public class ProgressBarUpdateUIThread {

 public static void main(String[] args) {
   Display display = new Display();
   Shell shell = new Shell(display);
   ProgressBar bar = new ProgressBar(shell, SWT.SMOOTH);
   bar.setBounds(10, 10, 200, 32);
   shell.open();
   for (int i = 0; i <= bar.getMaximum(); i++) {
     try {
       Thread.sleep(100);
     } catch (Throwable th) {
     }
     bar.setSelection(i);
   }
   while (!shell.isDisposed()) {
     if (!display.readAndDispatch())
       display.sleep();
   }
   display.dispose();
 }

}</source>





Using the SWT.INDETERMINATE style: the operation is happening but doesn"t know when it"ll finish.

   <source lang="java">

import org.eclipse.swt.SWT; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.ProgressBar; import org.eclipse.swt.widgets.Shell; public class ProgressBarExample {

 public static void main(String[] args) {
   Display display = new Display();
   Shell shell = new Shell(display);
   shell.setLayout(new GridLayout());
   // Create an indeterminate ProgressBar
   ProgressBar pb2 = new ProgressBar(shell, SWT.HORIZONTAL | SWT.INDETERMINATE);
   pb2.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
   shell.open();
   while (!shell.isDisposed()) {
     if (!display.readAndDispatch()) {
       display.sleep();
     }
   }
 }

}</source>





Using Thread to update ProgressBar value

   <source lang="java">

import org.eclipse.swt.SWT; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.ProgressBar; import org.eclipse.swt.widgets.Shell; public class ProgressBarExample {

 public static void main(String[] args) {
   Display display = new Display();
   Shell shell = new Shell(display);
   shell.setLayout(new GridLayout());
   // Create a smooth ProgressBar
   ProgressBar pb1 = new ProgressBar(shell, SWT.HORIZONTAL | SWT.SMOOTH);
   pb1.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
   pb1.setMinimum(0);
   pb1.setMaximum(30);
   // Start the first ProgressBar
   new LongRunningOperation(display, pb1).start();
   shell.open();
   while (!shell.isDisposed()) {
     if (!display.readAndDispatch()) {
       display.sleep();
     }
   }
 }

} class LongRunningOperation extends Thread {

 private Display display;
 private ProgressBar progressBar;
 public LongRunningOperation(Display display, ProgressBar progressBar) {
   this.display = display;
   this.progressBar = progressBar;
 }
 public void run() {
   for (int i = 0; i < 30; i++) {
     try {
       Thread.sleep(1000);
     } catch (InterruptedException e) {
     }
     display.asyncExec(new Runnable() {
       public void run() {
         if (progressBar.isDisposed())
           return;
         progressBar.setSelection(progressBar.getSelection() + 1);
       }
     });
   }
 }

}</source>