C'est simple dans ton code initial (celui que tu as indique ici)
Le rafraichissement de l'interface graphique ne pouvais pas se faire tant que ta bouche qui faisait le setValue pour la progress bar, était active. Il falait paralléliser d'une part ce qui faisait le setValue tout en rendant la main à l'interface graphique pour ne pas brider le système qui la rafaichit.
De plus si toutes les autres actions (par exemple d'autre boutons dans l'interface) sont inhibés car tout le temps machine est occupé par cette fameuse boucle.
Core Breaker 
-------------------------------
Réponse au message :
-------------------------------
>
> salut et merci
> ton exemple marche pas mal du tout mais pourrais tu m'expliquer pourquoi ca marche mieux en 'threadant' le travail de la scrollbar ?
>
>
> -------------------------------
> Réponse au message :
> -------------------------------
>
> >
> >
> > import java.awt.* ; > > import javax.swing.* ; > > import java.awt.event.* ; > > > > public class TestProgress extends JFrame implements ActionListener { > > > > public JProgressBar progressBar ; > > private JButton button ; > > private boolean end = false ; > > > > public TestProgress() { > > super( "JProgressBar test" ) ; > > frameInit() ; > > setDefaultCloseOperation(EXIT_ON_CLOSE); > > > > progressBar = new JProgressBar( 0 , 1000 ) ; > > progressBar.setStringPainted( true ) ; > > progressBar.setValue( 0 ) ; > > button = new JButton( "Start" ) ; > > button.addActionListener( this ) ; > > > > Container content = getContentPane() ; > > content.setLayout( new FlowLayout() ) ; > > content.add( progressBar ) ; > > content.add( button ) ; > > > > setLocation( 400 , 400 ) ; > > pack() ; > > setVisible( true ) ; > > > > } > > > > public void actionPerformed( ActionEvent e ) { > > final int min = progressBar.getMinimum() ; > > > > if ( end ) { > > progressBar.setValue( min ) ; > > end = false ; > > return ; > > } > > if( !running ) > > { > > final TestProgress thisFrame= this; > > new Thread(new Runnable() > > { > > int minval= min; > > TestProgress mFrame= thisFrame; > > public void run() > > { > > mFrame.setRun(); > > try { > > int max = mFrame.progressBar.getMaximum() ; > > for ( int i = minval ; i <= max ; i+=10 ) { > > Thread.sleep( 100 ) ; > > mFrame.progressBar.setValue( i ) ; > > } > > } catch ( InterruptedException ex ) { > > System.err.println( "Error : " + ex.getMessage() ) ; > > ex.printStackTrace() ; > > } > > } > > }).start(); > > } > > } > > > > private boolean running=false; > > > > public void setRun() > > { > > running= true; > > } > > > > public void setEnd() > > { > > end = true ; > > running= false; > > } > > > > public static void main( String [] args ) { > > new TestProgress() ; > > } > > } > > > > |
> >
Core Breaker 
> >
> >
> > -------------------------------
> > Réponse au message :
> > -------------------------------
> >
> > > Salut a tous
> > >
> > > comme le titre l'indique, j'ai un probleme avec une JProgressBar qui ne progresse pas.
> > >
> > > Y a t il un champion ici qui pourrait regarder cette source et me dire ce qui ne va pas. Ca fait un moment que je galere dessus et je ne comprends vraiment pas pourquoi ma barre de progression n'avance pas en fonction des setValue()
> > >
> > > je precise aussi que j'utilise comme JDK la 1.4.1 RC (la premiere 1.4.1 sortie)
> > >
> > > merci
> > >
> > > import java.awt.* ;
> > > import javax.swing.* ;
> > > import java.awt.event.* ;
> > >
> > > public class TestProgress extends JFrame implements ActionListener {
> > >
> > > private JProgressBar progressBar ;
> > > private JButton button ;
> > > private boolean end = false ;
> > >
> > > public TestProgress() {
> > > super( "JProgressBar test" ) ;
> > > frameInit() ;
> > > enableEvents( AWTEvent.WINDOW_EVENT_MASK ) ;
> > >
> > > progressBar = new JProgressBar( 0 , 1000 ) ;
> > > progressBar.setStringPainted( true ) ;
> > > progressBar.setValue( 0 ) ;
> > > button = new JButton( "Start" ) ;
> > > button.addActionListener( this ) ;
> > >
> > > Container content = getContentPane() ;
> > > content.setLayout( new FlowLayout() ) ;
> > > content.add( progressBar ) ;
> > > content.add( button ) ;
> > >
> > > addWindowListener(
> > > new WindowAdapter() {
> > > public void windowClosing( WindowEvent e ) {
> > > System.exit( 0 ) ;
> > > }
> > > }
> > > ) ;
> > >
> > > setLocation( 400 , 400 ) ;
> > > pack() ;
> > > setVisible( true ) ;
> > >
> > > }
> > >
> > > public void actionPerformed( ActionEvent e ) {
> > > int min = progressBar.getMinimum() ;
> > >
> > > if ( end ) {
> > > progressBar.setValue( min ) ;
> > > end = false ;
> > > return ;
> > > }
> > >
> > > try {
> > > int max = progressBar.getMaximum() ;
> > > for ( int i = min ; i <= max ; i+=10 ) {
> > > Thread.sleep( 100 ) ;
> > > progressBar.setValue( i ) ;
> > > }
> > > } catch ( InterruptedException ex ) {
> > > System.err.println( "Error : " + ex.getMessage() ) ;
> > > ex.printStackTrace() ;
> > > }
> > >
> > > end = true ;
> > > }
> > >
> > > public static void main( String [] args ) {
> > > new TestProgress() ;
> > > }
> > > }
> >
>