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

Agents de placement/Fenêtres Java Discussion :

Problème JFrame de progression


Sujet :

Agents de placement/Fenêtres Java

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre confirmé
    Inscrit en
    Mai 2008
    Messages
    126
    Détails du profil
    Informations forums :
    Inscription : Mai 2008
    Messages : 126
    Par défaut Problème JFrame de progression
    Bonjour,
    J'ai un problème avec une JFrame qui me sert à afficher ma progression.
    Les JFrame sont censé s'exécuter dans un Thread à part !! et même si je met mon code dans un autre Thread cela ne marche pas, ma JFrame reste bloqué lorsque mon applet execute du code !

    voila mon code de ma JFrame :

    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
     
    public class JInformation extends JFrame{
     
        public JInformation(JLabel label) {
        	super("Progression :");
            JPanel panel = new JPanel(new BorderLayout());
            JPanel prince = new JPanel(new FlowLayout());
            JProgressBar progressBar = new JProgressBar();
            progressBar.setIndeterminate(true); //permet de faire défile la progress bar en continue
            progressBar.setStringPainted(true);
            prince.add("North",label);
            prince.add("South",progressBar);
            panel.add(prince);
     
            this.setContentPane(panel);
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            this.setSize(250,75);
            Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
            this.setLocation((int)(screenSize.getWidth()-this.getWidth())/2, 0);
            this.setVisible(true);
        }
    }
    et dans mon code principale je fais :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
     
     
       JLabel label = new JLabel("Génération en cours ...");
       JInformation info = new JInformation(label);
       generation();
       label.setText("Traitement1");
       mafonction1();
       label.setText("Traitement2");
       mafonction2();
       label.setText("Traitement3");
       mafonction3();
       info.dispose();
    et là rien ne marche, mon label ne change pas de texte, même avec un repaint(), et de plus je vois que ma JProgressBar se bloque lors de l'éxécution du code principale. Ce qui n'est pas normal car elle est censé s'éxécuter dans un Thread à part !

    Quelqu'un aurait-il une idée ?

  2. #2
    Membre Expert
    Avatar de visiwi
    Profil pro
    Inscrit en
    Février 2008
    Messages
    1 052
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2008
    Messages : 1 052
    Par défaut
    Cela fonctionne, compile et lance cet exemple :
    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
    import java.awt.BorderLayout;
    import javax.swing.BorderFactory;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JProgressBar;
     
    public class JInformation extends JFrame{
     
    	public static void main(String[] args) {
    		JLabel label = new JLabel("Génération en cours ...");
    		   JInformation info = new JInformation(label);
    		   info.setVisible(true);
    		   simulation();
    		   label.setText("Traitement1");
    		   simulation();
    		   label.setText("Traitement2");
    		   simulation();
    		   label.setText("Traitement3");
    		   simulation();
    		   info.dispose();
    	}
     
    	private static void simulation() {
    		try {
    			Thread.sleep(2000);
    		} catch (InterruptedException e) {
    			e.printStackTrace();
    		}
    	}
     
        public JInformation(JLabel label) {
        	super("Progression :");
            JPanel panel = new JPanel(new BorderLayout());
            panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
            JProgressBar progressBar = new JProgressBar();
            progressBar.setIndeterminate(true);
            progressBar.setStringPainted(true);
            panel.add("North",label);
            panel.add("South",progressBar);
            this.setContentPane(panel);
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            this.setSize(250,75);
            this.setLocationRelativeTo(null);
        }
     
    }
    Cela dit, il est préférable d'exécuter les méthodes Swing, que tu utilise pour modifier ton interface dynamiquement, depuis l'EDT (Event Dispatch Thread).
    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
    import java.awt.BorderLayout;
    import javax.swing.BorderFactory;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JProgressBar;
    import javax.swing.SwingUtilities;
     
    public class JInformation extends JFrame {
     
    	public static void main(String[] args) {
    		JLabel label = new JLabel("Génération en cours ...");
    		JInformation info = new JInformation(label);
    		info.setVisible(true);
    		simulation();
    		changeText("Traitement1", label);
    		simulation();
    		changeText("Traitement2", label);
    		simulation();
    		changeText("Traitement3", label);
    		simulation();
    		info.dispose();
    	}
     
    	private static void simulation() {
    		try {
    			Thread.sleep(2000);
    		} catch (InterruptedException e) {
    			e.printStackTrace();
    		}
    	}
     
    	public JInformation(JLabel label) {
    		super("Progression :");
    		JPanel panel = new JPanel(new BorderLayout());
    		panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
    		JProgressBar progressBar = new JProgressBar();
    		progressBar.setIndeterminate(true);
    		progressBar.setStringPainted(true);
    		panel.add("North", label);
    		panel.add("South", progressBar);
    		this.setContentPane(panel);
    		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		this.setSize(250, 75);
    		this.setLocationRelativeTo(null);
    	}
     
    	private static void changeText(final String s, final JLabel label) {
    		SwingUtilities.invokeLater(new Runnable() {
    			@Override
    			public void run() {
    				label.setText(s);
    			}
    		});
    	}
     
    }

  3. #3
    Membre confirmé
    Inscrit en
    Mai 2008
    Messages
    126
    Détails du profil
    Informations forums :
    Inscription : Mai 2008
    Messages : 126
    Par défaut
    merci de ta réponse mais ça ne marche toujours pas même en appelant avec la méthode Swing.

    Cela vient peut être du fait que les fonctions que j'appelle sont dans des Applets ?? je vous met le code principale plus en détaille :

    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
     
    public class myApplet extends Applet(){
     
        private Applet1 applet1;
        private Applet1 applet2;
     
        public void init(){
            applet1 = new Applet1();
            applet2 = new Applet2();
        }
     
        public void traitementPrincipal(){
            JLabel label = new JLabel("Génération en cours ...");
            JInformation info = new JInformation(label);
            generation();
            label.setText("Traitement1");
            applet1.mafonction1();
            label.setText("Traitement2");
            applet2.mafonction2();
            info.dispose();
        }
    }
    cela change-t-il quelque chose ?

  4. #4
    Membre Expert
    Avatar de visiwi
    Profil pro
    Inscrit en
    Février 2008
    Messages
    1 052
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2008
    Messages : 1 052
    Par défaut
    Le problème ne serait-il pas ailleurs, voici la progression dans un applet, cela fonctionne :
    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
    import java.applet.Applet;
    import javax.swing.JLabel;
     
    public class myApplet extends Applet {
     
        public void traitementPrincipal(){
            JLabel label = new JLabel("Génération en cours ...");
            JInformation info = new JInformation(label);
            //generation();
            label.setText("Traitement1");
            info.simulation();
            label.setText("Traitement2");
            info.simulation();
            info.dispose();
        }
     
    	@Override
    	public void init() {
    		traitementPrincipal();
    	}    
     
    }
    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
    import java.awt.BorderLayout;
    import java.awt.Dimension;
    import java.awt.FlowLayout;
    import java.awt.Toolkit;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JProgressBar;
     
    public class JInformation extends JFrame {
     
        public JInformation(JLabel label) {
        	super("Progression :");
            JPanel panel = new JPanel(new BorderLayout());
            JPanel prince = new JPanel(new FlowLayout());
            JProgressBar progressBar = new JProgressBar();
            progressBar.setIndeterminate(true); //permet de faire défile la progress bar en continue
            progressBar.setStringPainted(true);
            prince.add("North",label);
            prince.add("South",progressBar);
            panel.add(prince);
     
            this.setContentPane(panel);
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            this.setSize(250,75);
            Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
            this.setLocation((int)(screenSize.getWidth()-this.getWidth())/2, 0);
            this.setVisible(true);
        }
     
        public void simulation() {
    		try {
    			Thread.sleep(2000);
    		} catch (InterruptedException e) {
    			e.printStackTrace();
    		}
    	}
     
    }
    Si tu le souhaite, post la totalité de ton code.

  5. #5
    Membre confirmé
    Inscrit en
    Mai 2008
    Messages
    126
    Détails du profil
    Informations forums :
    Inscription : Mai 2008
    Messages : 126
    Par défaut
    sorry mais je ne peux pas déposer tout mon code mais en fait dans mon init() je fais juste l'instanciation de mes applets, la fonction traitement principale est appelé depuis ma page en javascript :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    document.myApplet.traitementPrincipal();
    je voit vraiment pas ou cela peut bugger :S

  6. #6
    Membre Expert
    Avatar de visiwi
    Profil pro
    Inscrit en
    Février 2008
    Messages
    1 052
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2008
    Messages : 1 052
    Par défaut
    Et bien, sans le code je ne peux pas t'aider...
    Mais le problème est ailleurs, peut-être dans les méthodes generation(), mafonction1() et mafonction2(). Mais le traitement se finit-il (même si graphiquement ce n'est pas mis à jours) ou bien ton apllet est-il complètement bloqué ?

Discussions similaires

  1. Problème de restore progression
    Par thom48 dans le forum Administration
    Réponses: 16
    Dernier message: 25/03/2010, 17h21
  2. Problème JFrame "animée" et ActionPerformed
    Par allan000 dans le forum Agents de placement/Fenêtres
    Réponses: 1
    Dernier message: 22/11/2008, 14h32
  3. Problème d'update progress
    Par yohanndos dans le forum ASP.NET
    Réponses: 3
    Dernier message: 01/09/2008, 09h19
  4. Problème avec la progression de TGauge
    Par Marley_T dans le forum Langage
    Réponses: 5
    Dernier message: 20/03/2008, 17h13
  5. probléme du composant progress bar
    Par ouadie99 dans le forum Windows Forms
    Réponses: 4
    Dernier message: 06/03/2008, 14h43

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