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
|
public class IndeterminateProgressBar extends SwingWorker<Integer, String> {
private JFrame jf;
private JProgressBar jpb;
private TacheAction tache;
private JLabel jl;
private String texte;
//************************************
//CONSTRUCTEURS
//************************************
public IndeterminateProgressBar(TacheAction tache) {
this.tache = tache;
this.jl = new JLabel();
createAndshowGui(null);
}
public IndeterminateProgressBar(TacheAction tache, String texte) {
this.tache = tache;
this.texte = texte;
this.jl = new JLabel(this.texte);
createAndshowGui(null);
}
public IndeterminateProgressBar(String texte) {
this.texte = texte;
this.jl = new JLabel();
createAndshowGui(null);
}
//************************************
//METHODES PRIVEE
//************************************
private void createAndshowGui(String explication) {
//Paramétrage de la Frame
jf = new JFrame();
jf.setAlwaysOnTop(true);
//Initialisation de la progressbar
jpb = new JProgressBar();
jpb.setIndeterminate(true);
jpb.setStringPainted(false);//Permet de garder la même hauteur si un texte est affiché ou non
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.PAGE_AXIS));
mainPanel.setPreferredSize(new Dimension(400, 70));
jl.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
jpb.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
jl.setAlignmentX(Component.CENTER_ALIGNMENT);
mainPanel.add(this.jl);
mainPanel.add(this.jpb);
jf.setContentPane(mainPanel);
jf.pack();
jf.setLocationRelativeTo(jf.getParent()); // Centrage de la frame
jf.setVisible(true);
}
@Override
protected Integer doInBackground() throws Exception {
if (tache != null)
this.tache.aFaire();
else{
System.out.println("Début traitement");
wait(5000);
System.out.println("Fin traitement");
}
return null;
}
@Override
public void done() {
// Les ressources utilisées par la fenêtre ainsi que
// ses enfants sont détruites et récupérées
System.out.println("Terminé");
jf.dispose();
}
public static void main(String... args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
/* Démarrage de l'interface graphique et du SwingWorker. */
IndeterminateProgressBar demo = new IndeterminateProgressBar("Test");
demo.execute();
}
});
}
} |
Partager