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 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
|
ort javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class JProgress2 extends JFrame implements ActionListener, Runnable
{
private JProgressBar progress;
private boolean isStarted;
private JButton bouton;
private int value;
public JProgress2 ()
{
super ("JProgressBar");
this.isStarted = false;
this.value = 0;
this.progress = new JProgressBar (0, 100);
this.progress.setStringPainted (true);
this.getContentPane ().add (this.progress);
bouton = new JButton ("start");
bouton.addActionListener (this);
this.getContentPane ().add (bouton, BorderLayout.NORTH);
this.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
this.setLocation (200, 200);
this.pack ();
this.setVisible (true);
}
// methode de ActionListener appelle suite au clic sur le bouton
public void actionPerformed (ActionEvent e)
{
if ( this.isStarted ) // effectue un reset
{ this.progress.setValue (0);
this.bouton.setText ("start");
this.value = 0;
}
else // effectue le start
{ this.bouton.setText ("reset");
this.bouton.setEnabled (false);
// ici on fait l'appel a la methode de lanceLongTraitement
// rappel : nous sommes dans le processus d'evenement
this.lanceLongTraitement ();
}
this.isStarted = ! this.isStarted;
}
// cree un nouveau Thread et appelle la methode start
// Cf une documentation sur les Thread si vous ne comprennez pas le
// mecanisme ....
public void lanceLongTraitement ()
{
Thread t = new Thread (this);
t.start ();
// maintenant nous rendons la main au processus d'evenement
}
// methode de l'interface Runnable
// lance un nouveau thread qui va executer le code de la methode longTraitement
public void run ()
{ this.longTraitement ();
}
public void longTraitement ()
{ // execute la succession d'operation ...
// on est plus dans le processus d'evenement mais dans le nouveau Thread
for (int i = 0; i < 100; i++)
{ this.uneOperation ();
// maintenant on appelle la methode pour mettre a jour la barre
// de progression
this.majProgress ();
}
}
// methode qui effectue une pause pour simuler une operation
private synchronized void uneOperation ()
{ try
{ this.wait (100);
}
catch (InterruptedException e)
{
}
}
// methode qui met a jour la JProgressBar par le processus d'evenement
// Pourquoi obliger l'execution de cette methode par le processus d'evenement ?
// -> Cf : la docs du tutoriel de Sun section : "Threads and Swing"
// <a href="http://java.sun.com/docs/books/tutorial/uiswing/mini/threads.html" target="_blank">http://java.sun.com/docs/books/tutor...i/threads.html</a>
public void majProgress ()
{ if ( SwingUtilities.isEventDispatchThread () )
{ progress.setValue (++value);
}
else
{ Runnable callMAJ = new Runnable ()
{ public void run ()
{ majProgress ();
}
};
SwingUtilities.invokeLater (callMAJ);
}
}
public static void main (String argv [])
{ new JProgress2 ();
}
} |
Partager