1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
| import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ProgressBar implements Runnable {
private JWindow progressBarFrame; // Frame d'affichage de la barre de creation
protected ActionInterface action; // action en cours d'execution
private JProgressBar progressBar; // barre de progression
private Thread waitingThread; // thread permettant de gerer l'evolution de l'action
public ProgressBar(ActionInterface action)
{
this.action = action;
ProgressBarBox progressBarBox = new ProgressBarBox();
progressBar = progressBarBox.getProgressBar();
progressBarFrame = progressBarBox.getProgressBarFrame();
waitingThread= new Thread(this);
waitingThread.start();
action.launch();
}
public void run()
{
int countProgress = 0;
while (!action.getActionStatus()) {
if (countProgress >= 100) countProgress = 0;
else countProgress=countProgress+1;
progressBar.setValue(countProgress);
try {waitingThread.sleep(20);}
catch(Exception e){}
}
progressBarFrame.dispose();
}
private class ProgressBarBox {
private int scale = 100; // echelle de la barre de progression
private JWindow progressBarFrame; // fenetre de la barre de progression
private JProgressBar progressBar; // la barre de progression
private ProgressBarBox()
{
progressBarFrame = new JWindow();
JPanel panelProgressBar = new JPanel();
JLabel texte = new JLabel("Please wait, process in progress");
progressBar = new JProgressBar(0, scale);
panelProgressBar.add("Center", progressBar);
panelProgressBar.add("Center", texte);
progressBarFrame.getContentPane().add(BorderLayout.CENTER, panelProgressBar);
progressBarFrame.setSize(275,85);
progressBarFrame.setVisible(true);
}
private JWindow getProgressBarFrame() {return progressBarFrame;}
private JProgressBar getProgressBar() {return progressBar;}
}
public static void main(String... args) {
Runnable gui = new Runnable() {
public void run() {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(400, 300);
JButton btStart = new JButton("Start");
f.add(btStart, BorderLayout.NORTH);
btStart.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ProgressBar progressBar1 = new ProgressBar(new ActionInterface());
}
});
f.setVisible(true);
}
};
//GUI must start on EventDispatchThread:
SwingUtilities.invokeLater(gui);
}
}
class ActionInterface implements Runnable {
private boolean actionStatus;
public void launch() {
new Thread(this).start();
}
boolean getActionStatus() {
return actionStatus;
}
public void run() {
for (int i = 0; i < 100; i++) {
try {
Thread.sleep(20);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
actionStatus = true;
}
} |
Partager