devstory

Le Tutoriel de Java SWT ProgressBar

  1. SWT ProgressBar
  2. Exemple de SWT ProgressBar
  3. ProgressBar et Thread

1. SWT ProgressBar

ProgressBar est un contrôle d'interface utilisateur qui permet de visualiser la progression de toutes les opérations dans vos applications SWT.
La figure ci-dessous illustre trois ProgressBar, deux premiers ProgressBar décrivent la progression du travail avec un volume défini. Le dernier ProgressBar décrit l'évolution du travail avec un volume indéfini.
ProgressBar vertical:
Les style peuvent être appliqués au ProgressBar:
  • SMOOTH
  • HORIZONTAL
  • VERTICAL
  • INDETERMINATE
// Horizontal ProgressBar
ProgressBar progressBar1 = new ProgressBar(shell, SWT.NULL);
ProgressBar progressBar2 = new ProgressBar(shell, SWT.SMOOTH);
ProgressBar progressBar3 = new ProgressBar(shell, SWT.INDETERMINATE);

// Vertical ProgressBar
ProgressBar progressBar4 = new ProgressBar(shell, SWT.VERTICAL | SWT.SMOOTH);

2. Exemple de SWT ProgressBar

Par exemple: Créez ProgressBar avec différents styles :
ProgressDemo.java
package org.o7planning.swt.progressbar;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.ProgressBar;
import org.eclipse.swt.widgets.Shell;

public class ProgressBarDemo {
    public ProgressBarDemo() {
        Display display = new Display();
        Shell shell = new Shell(display);
        shell.setText("SWT ProgressBar (o7planning.org)");
        shell.setSize(450, 200);

        ProgressBar progressBar1 = new ProgressBar(shell, SWT.NULL);
        ProgressBar progressBar2 = new ProgressBar(shell, SWT.SMOOTH);
        ProgressBar progressBar3 = new ProgressBar(shell, SWT.INDETERMINATE);

        progressBar1.setMinimum(30);
        progressBar1.setMaximum(250);
        progressBar1.setSelection(160);
        
        progressBar2.setMinimum(30);
        progressBar2.setMaximum(250);
        progressBar2.setSelection(200);        

        progressBar1.setBounds(140, 10, 200, 20);
        progressBar2.setBounds(140, 40, 200, 20);
        progressBar3.setBounds(140, 70, 200, 20);

        Label label1 = new Label(shell, SWT.NULL);
        label1.setText("(default)");
        Label label2 = new Label(shell, SWT.NULL);
        label2.setText("SWT.SMOOTH");
        Label label3 = new Label(shell, SWT.NULL);
        label3.setText("SWT.INDETERMINATE");

        label1.setAlignment(SWT.RIGHT);
        label2.setAlignment(SWT.RIGHT);
        label3.setAlignment(SWT.RIGHT);

        label1.setBounds(10, 10, 120, 20);
        label2.setBounds(10, 40, 120, 20);
        label3.setBounds(10, 70, 120, 20);

        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
        display.dispose();
    }
    public static void main(String[] args) {
        new ProgressBarDemo();
    }
}

3. ProgressBar et Thread

Vous pouvez créer un objet du fil (Thread) afin d'effectuer certaines tâches, telles que la copie de la liste des fichiers. La copie nécessite de prendre un certain temps, vous devez utiliser ProgressBar pour afficher le pourcentage du travail déjà fait.
ProgressBarCopyDemo.java
package org.o7planning.swt.progressbar;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.ProgressBar;
import org.eclipse.swt.widgets.Shell;

public class ProgressBarCopyDemo {
    private CopyThread copyThread = null;
    public ProgressBarCopyDemo() {
        Display display = new Display();
        Shell shell = new Shell(display);
        shell.setText("SWT ProgressBar (o7planning.org)");
        shell.setSize(450, 200);
        shell.setLayout(null);

        ProgressBar progressBar = new ProgressBar(shell, SWT.NONE);
        progressBar.setBounds(10, 23, 350, 17);

        Label labelInfo = new Label(shell, SWT.NONE);
        labelInfo.setBounds(10, 46, 350, 15);
        labelInfo.setText(" ...");

        // Button Copy
        Button buttonCopy = new Button(shell, SWT.NONE);
        buttonCopy.setBounds(122, 67, 75, 25);
        buttonCopy.setText("Copy");
        // Button Cancel
        Button buttonCancel = new Button(shell, SWT.NONE);
        buttonCancel.setBounds(200, 67, 75, 25);
        buttonCancel.setText("Cancel");
        buttonCancel.setEnabled(false);

        buttonCopy.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                copyThread = new CopyThread(display, progressBar, labelInfo, buttonCopy, buttonCancel);
                copyThread.start();
            }
        });
        buttonCancel.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                if (copyThread != null) {
                    copyThread.cancel();
                }
            }
        });
        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
        display.dispose();
    }
    public static void main(String[] args) {
        new ProgressBarCopyDemo();
    }
}
CopyThread.java
package org.o7planning.swt.progressbar;

import java.io.File;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.ProgressBar;

public class CopyThread extends Thread {
    private Display display;
    private ProgressBar progressBar;
    private Button buttonCopy;
    private Button buttonCancel;
    private Label labelInfo;
    private boolean cancel;

    public CopyThread(Display display, ProgressBar progressBar, //
            Label labelInfo, Button buttonCopy, Button buttonCancel) {
        this.display = display;
        this.progressBar = progressBar;
        this.buttonCopy = buttonCopy;
        this.buttonCancel = buttonCancel;
        this.labelInfo = labelInfo;
    }
    @Override
    public void run() {
        if (display.isDisposed()) {
            return;
        }
        this.updateGUIWhenStart();

        // Copy All file In C:/Windows
        File dir = new File("C:/Windows");
        File[] files = dir.listFiles();
        int count = files.length;

        int i = 0;
        for (File file : files) {
            if (cancel) {
                break;
            }
            i++;
            if (file.isFile()) {
                this.copy(file);
            } else {
                continue;
            }
            this.updateGUIInProgress(file, i, count);
        }
        this.updateGUIWhenFinish();
    }
    private void copy(File file) {
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
        }
    }
    private void updateGUIWhenStart() {
        display.asyncExec(new Runnable() {
            @Override
            public void run() {
                buttonCopy.setEnabled(false);
                buttonCancel.setEnabled(true);
            }
        });
    }
    private void updateGUIWhenFinish() {
        display.asyncExec(new Runnable() {
            @Override
            public void run() {
                buttonCopy.setEnabled(true);
                buttonCancel.setEnabled(false);
                progressBar.setSelection(0);
                progressBar.setMaximum(1);
                if (cancel) {
                    labelInfo.setText("Cancelled!");
                } else {
                    labelInfo.setText("Finished!");
                }
            }
        });
    }
    private void updateGUIInProgress(File file, int value, int count) {
        display.asyncExec(new Runnable() {
            @Override
            public void run() {
                labelInfo.setText("Copying file: " + file.getAbsolutePath());
                progressBar.setMaximum(count);
                progressBar.setSelection(value);
            }
        });
    }
    public void cancel() {
        this.cancel = true;
    }
}