IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

AWT/Swing Java Discussion :

[JProgressBar] Utilisation d'un thread


Sujet :

AWT/Swing Java

  1. #1
    Expert confirmé
    Avatar de GLDavid
    Homme Profil pro
    Service Delivery Manager
    Inscrit en
    Janvier 2003
    Messages
    2 852
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 47
    Localisation : France, Seine Saint Denis (Île de France)

    Informations professionnelles :
    Activité : Service Delivery Manager
    Secteur : Industrie Pharmaceutique

    Informations forums :
    Inscription : Janvier 2003
    Messages : 2 852
    Points : 4 759
    Points
    4 759
    Par défaut [JProgressBar] Utilisation d'un thread
    Bonjour

    Suite à mes déboires d'un JProgressBar qui ne se remplissait pas, j'ai fouillé sur le site (à moins que l'un d'entre vous ne me l'ai recommandé) et je suis tombé sur ce lien très intéressant: http://alwin.developpez.com/tutorial/JavaThread/.
    Sur ce lien est expliqué la progression d'un JProgressBar en utilisant un thread. J'ai donc voulu l'implémenter.
    Voici la call-back d'un JButton qui déclenche l'opération de progression:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
     
    jButton.addActionListener(new ActionListener(){
    			public void actionPerformed(ActionEvent evt){
    				WaitDialog wait = new WaitDialog();
    					wait.setAlwaysOnTop(true);
    					String s = (String)OntologyChooser.jComboBox.getSelectedItem();
    					SearchOntology so = new SearchOntology(s);
    					Iterator i = Makanko.proteinList.iterator();
    					int size = Makanko.proteinList.size();
    					int cpt = 0;
    					while(i.hasNext()){
    						Protein protein = (Protein)i.next();
    						cpt++;
    						nb = (int)((cpt*100)/size);
    						System.out.println(""+protein.nom+" "+nb);
    						protein.geneontology_accession = so.getOntologyProtein(protein.nom);
    						if(protein.geneontology_accession.compareTo("")!=0){
    							protein.ontology_definition = so.getOntologyDefinition(protein.geneontology_accession);
    						}
    						Thread t = new Thread() {
    							public void run() {
    					        	LongTraitement action = new LongTraitement();
    					            action.traitementLong();
    					        }
    					      };
    					      t.start();
    					}
    					wait.setVisible(false);
    					wait.dispose();
    					exitForm(evt);
    			}});
    Voici la classe LongTraitement:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
     
    public class LongTraitement {
     
        public void traitementLong() {
            for( int i = 0; i < 100; i++ ) {
                WaitDialog.jProgressBar.setValue(OntologyChooser.nb);
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {}
            }
        }
    }
    et enfin, la JDialog qui contient la barre de remplissage:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
     
    import java.awt.BorderLayout;
    import java.awt.Dimension;
    import java.awt.Toolkit;
     
    import javax.swing.JPanel;
    import javax.swing.JDialog;
    import javax.swing.JLabel;
    import javax.swing.JProgressBar;
     
    public class WaitDialog extends JDialog{
     
    	public static JPanel jContentPane = null;
    	private JLabel jLabel = null;
    	public static JProgressBar jProgressBar = null;
     
    	/**
             * This is the default constructor
             */
    	public WaitDialog() {
    		super();
    		initialize();
    	}
     
    	/**
             * This method initializes this
             * 
             * @return void
             */
    	private void initialize() {
    		this.setSize(300, 200);
    		this.setTitle("Processing");
    		this.setContentPane(getJContentPane());
    		this.pack();
    		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    		this.setLocation((screenSize.width - 512) / 2, (screenSize.height - 512) / 2);
    		this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    		//this.setDefaultLookAndFeelDecorated(false);
    		//this.setUndecorated(true);
    		this.setVisible(true);
    	}
     
    	/**
             * This method initializes jContentPane
             * 
             * @return javax.swing.JPanel
             */
    	private JPanel getJContentPane() {
    		if (jContentPane == null) {
    			jLabel = new JLabel();
    			jLabel.setText("Please, wait while procesing");
    			jContentPane = new JPanel();
    			jContentPane.setLayout(new BorderLayout());
    			jContentPane.add(jLabel, java.awt.BorderLayout.NORTH);
    			jContentPane.add(getJProgressBar(), java.awt.BorderLayout.CENTER);
    		}
    		return jContentPane;
    	}
     
    	/**
             * This method initializes jProgressBar 
             *      
             * @return javax.swing.JProgressBar     
             */
    	private JProgressBar getJProgressBar() {
    		if (jProgressBar == null) {
    			jProgressBar = new JProgressBar();
    			jProgressBar.setStringPainted(true);
    			jProgressBar.setMinimum(0);
    			jProgressBar.setMaximum(100);
    			jProgressBar.setValue(0);
    		}
    		return jProgressBar;
    	}
     
    }
    Mon problème est que ma JDialog apparait mais, la JProgressbar n'apparaît pas et ne se remplit pas alors que le traitement est correct.
    Merci d'avance de vos réponses.

    @++
    GLDavid
    Consultez la FAQ Perl ainsi que mes cours de Perl.
    N'oubliez pas les balises code ni le tag

    Je ne répond à aucune question technique par MP.

  2. #2
    Membre éclairé Avatar de Pollux
    Profil pro
    Inscrit en
    Avril 2005
    Messages
    706
    Détails du profil
    Informations personnelles :
    Âge : 38
    Localisation : Suisse

    Informations forums :
    Inscription : Avril 2005
    Messages : 706
    Points : 680
    Points
    680
    Par défaut
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    public static JProgressBar jProgressBar = null;
    Une variable statique n'est pas liée à l'instance de l'objet.

    A mon avis il faudrait changer le mot-clé static en private et faire une méthode setProgres(int valeur) où là tu pourras changer la valeur de ta JProgressBar en rapport avec ton objet
    Pour chaque langage existe une faq / N'oubliez pas de lire les règles du forum

  3. #3
    Expert confirmé
    Avatar de GLDavid
    Homme Profil pro
    Service Delivery Manager
    Inscrit en
    Janvier 2003
    Messages
    2 852
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 47
    Localisation : France, Seine Saint Denis (Île de France)

    Informations professionnelles :
    Activité : Service Delivery Manager
    Secteur : Industrie Pharmaceutique

    Informations forums :
    Inscription : Janvier 2003
    Messages : 2 852
    Points : 4 759
    Points
    4 759
    Par défaut
    Bonjour pollux007

    J'ai réimplémenté ma classe JDialog en prenant compte de ta remarque (merci d'ailleurs de ta réponse):
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
     
    import java.awt.BorderLayout;
    import java.awt.Dimension;
    import java.awt.Toolkit;
     
    import javax.swing.JPanel;
    import javax.swing.JDialog;
    import javax.swing.JLabel;
    import javax.swing.JProgressBar;
     
    public class WaitDialog extends JDialog{
     
    	public static JPanel jContentPane;
    	private JLabel jLabel = null;
    	public static JProgressBar jProgressBar;
     
    	/**
             * This is the default constructor
             */
    	public WaitDialog() {
    		super();
    		initialize();
    	}
     
    	/**
             * This method initializes this
             * 
             * @return void
             */
    	private void initialize() {
    		this.setSize(300, 200);
    		this.setTitle("Processing");
    		//this.setContentPane(getJContentPane());
    		this.getContentPane().add(getJContentPane());
    		this.pack();
    		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    		this.setLocation((screenSize.width - 512) / 2, (screenSize.height - 512) / 2);
    		this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    		//this.setDefaultLookAndFeelDecorated(false);
    		//this.setUndecorated(true);
    		this.setVisible(true);
    	}
     
    	/**
             * This method initializes jContentPane
             * 
             * @return javax.swing.JPanel
             */
    	private JPanel getJContentPane() {
    		if (jContentPane == null) {
    			jLabel = new JLabel();
    			jLabel.setText("Please, wait while processing");
    			jContentPane = new JPanel();
    			jContentPane.setLayout(new BorderLayout());
    			jContentPane.add(jLabel, java.awt.BorderLayout.NORTH);
    			jContentPane.add(getJProgressBar(), java.awt.BorderLayout.CENTER);
    		}
    		return jContentPane;
    	}
     
    	/**
             * This method initializes jProgressBar 
             *      
             * @return javax.swing.JProgressBar     
             */
    	private JProgressBar getJProgressBar() {
    		if (jProgressBar == null) {
    			jProgressBar = new JProgressBar();
    			jProgressBar.setStringPainted(true);
    			jProgressBar.setMinimum(0);
    			jProgressBar.setMaximum(100);
    			jProgressBar.setValue(0);
    		}
    		return jProgressBar;
    	}
     
    }
    Seulement, j'ai toujours le même problème. Une remarque supplémentaire : lors de l'ouverture de ma JDialog, rien n'apparaît, même pas le JLabel.

    Merci encore de vos réponses.

    @++
    GLDavid
    Consultez la FAQ Perl ainsi que mes cours de Perl.
    N'oubliez pas les balises code ni le tag

    Je ne répond à aucune question technique par MP.

  4. #4
    Expert confirmé
    Avatar de GLDavid
    Homme Profil pro
    Service Delivery Manager
    Inscrit en
    Janvier 2003
    Messages
    2 852
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 47
    Localisation : France, Seine Saint Denis (Île de France)

    Informations professionnelles :
    Activité : Service Delivery Manager
    Secteur : Industrie Pharmaceutique

    Informations forums :
    Inscription : Janvier 2003
    Messages : 2 852
    Points : 4 759
    Points
    4 759
    Par défaut
    Petite remarque: j'ai écrit un main dans ma JDialog pour voir comment elle apparaît. Or, elle apparaît au complet avec JLabel et JProgressBar inclus.
    Donc, je dois avoir un problème d'implémentation dans la call-back de mon bouton, non ?

    @++
    GLDavid
    Consultez la FAQ Perl ainsi que mes cours de Perl.
    N'oubliez pas les balises code ni le tag

    Je ne répond à aucune question technique par MP.

  5. #5
    Expert confirmé
    Avatar de GLDavid
    Homme Profil pro
    Service Delivery Manager
    Inscrit en
    Janvier 2003
    Messages
    2 852
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 47
    Localisation : France, Seine Saint Denis (Île de France)

    Informations professionnelles :
    Activité : Service Delivery Manager
    Secteur : Industrie Pharmaceutique

    Informations forums :
    Inscription : Janvier 2003
    Messages : 2 852
    Points : 4 759
    Points
    4 759
    Par défaut
    2ème observation. Dans la call-back de mon bouton, si je n'efface pas ma fenêtre à la fin du processus, j'observe que la barre apparaît finalement complète ainsi que le Label.
    Donc, je ne comprend pas du tout pourquoi lors du remplissage, mes widgets (JLabel et JProgressBar) n'apparaîssent pas.

    @++
    GLDavid
    Consultez la FAQ Perl ainsi que mes cours de Perl.
    N'oubliez pas les balises code ni le tag

    Je ne répond à aucune question technique par MP.

  6. #6
    Expert confirmé
    Avatar de GLDavid
    Homme Profil pro
    Service Delivery Manager
    Inscrit en
    Janvier 2003
    Messages
    2 852
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 47
    Localisation : France, Seine Saint Denis (Île de France)

    Informations professionnelles :
    Activité : Service Delivery Manager
    Secteur : Industrie Pharmaceutique

    Informations forums :
    Inscription : Janvier 2003
    Messages : 2 852
    Points : 4 759
    Points
    4 759
    Par défaut
    Ok, j'ai résolu mon problème. Il suffisait de réécrire mon thread, fort mal placé comme me l'avait recommandé un précédent posteur :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
     
    jButton.addActionListener(new ActionListener(){
    			public void actionPerformed(ActionEvent evt){
    				switch(which_class){
    				case 0:{
    					thread = new Thread() {
    						public void run() {
    							WaitDialog wait = new WaitDialog();
    							wait.setAlwaysOnTop(true);
    							String s = (String)OntologyChooser.jComboBox.getSelectedItem();
    							SearchOntology so = new SearchOntology(s);
    							Iterator i = Makanko.proteinList.iterator();
    							int size = Makanko.proteinList.size();
    							int cpt = 0;
    							while(i.hasNext()){
    								Protein protein = (Protein)i.next();
    								cpt++;
    								nb = (int)((cpt*100)/size);
    								protein.geneontology_accession = so.getOntologyProtein(protein.nom);
    								if(protein.geneontology_accession.compareTo("")!=0){
    									protein.ontology_definition = so.getOntologyDefinition(protein.geneontology_accession);
    								}
    								LongTraitement action = new LongTraitement();
    					            action.traitementLong();
    							}
    							wait.setVisible(false);
    							wait.dispose();
    				        }
    				      };
    				      thread.start();
    				      exitForm(evt);
    				}
    				break;
    				case 1:{
    					thread = new Thread() {
    						public void run() {
    							WaitDialog wait = new WaitDialog();
    							wait.setAlwaysOnTop(true);
    							String s = (String)OntologyChooser.jComboBox.getSelectedItem();
    							SearchOntology so = new SearchOntology(s);
    							Iterator i = Sampo.proteinList.iterator();
    							int size = Sampo.proteinList.size();
    							int cpt = 0;
    							while(i.hasNext()){
    								Protein protein = (Protein)i.next();
    								cpt++;
    								nb = (int)((cpt*100)/size);
    								protein.geneontology_accession = so.getOntologyProtein(protein.nom);
    								if(protein.geneontology_accession.compareTo("")!=0){
    									protein.ontology_definition = so.getOntologyDefinition(protein.geneontology_accession);
    								}
    								LongTraitement action = new LongTraitement();
    					            action.traitementLong();
    							}
    							wait.setVisible(false);
    						    wait.dispose();
    				        }
    				      };
    				      thread.start();
    				      exitForm(evt);
    				}
    				break;
    				}
    			}});
    Maintenant, ça marche impeccable.

    Merci à vous tous.

    @++
    GLDavid
    Consultez la FAQ Perl ainsi que mes cours de Perl.
    N'oubliez pas les balises code ni le tag

    Je ne répond à aucune question technique par MP.

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Réponses: 2
    Dernier message: 04/09/2007, 22h26
  2. Utilisation d'un thread pour faire une pause.
    Par ropabo dans le forum Concurrence et multi-thread
    Réponses: 5
    Dernier message: 11/06/2006, 14h28
  3. utilisation du Multi threading
    Par chti_juanito dans le forum Concurrence et multi-thread
    Réponses: 3
    Dernier message: 30/05/2006, 10h20
  4. [C# 2.0][Form]Comment utiliser les Delegates & Threading
    Par Tips dans le forum Windows Forms
    Réponses: 8
    Dernier message: 08/01/2006, 14h22

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo