Bonjour à tous,
Je débarque sur les threads, et j'essaye d'en utiliser un pour afficher une barre de progression.
Je souhaite lancer le thread au début du chargement, et l'arrêter à la fin.

Voici donc ma classe :
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
 
public class MyThread extends Thread {
  private JFrame jf = null;
 
  public MyThread (JFrame jf) {
    this.jf = jf; // interface principale
  }
 
  public void run() {
    while (true) {
      ((MonInterface)jf).mpb.maBarre.setValue (((MonInterface)jf).chargement); // où chargement est le pourcentage courant
      ((MonInterface)jf).mpb.maBarre.updateUI();
    }
  }
}
Dans mon interface principale, je met à jour ma variable qui représente le pourcentage, et mon thread est sensé le surveiller, et mettre à jour la barre en fonction :

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
 
    myThread = new MyThread(this);
    myThread.start();
    chargement = 10;
... traitement...
    chargement = 20;
... traitement...
    chargement = 40;
... traitement...
    chargement = 60;
... traitement...
    chargement = 80;
... traitement...
    chargement = 100;
    myThread.interrupt();

Mon pb est que le thread semble ne jamais s'arrêter, l'appli rame, la barre de progression scintille et reste à 100, et j'ai des Exceptions de ce type :

Exception in thread "Thread-3" java.lang.NullPointerException
at javax.swing.plaf.basic.BasicProgressBarUI.uninstallListeners(Unknown Source)
at javax.swing.plaf.basic.BasicProgressBarUI.uninstallUI(Unknown Source)
at javax.swing.JComponent.setUI(Unknown Source)
at javax.swing.JProgressBar.setUI(Unknown Source)
at javax.swing.JProgressBar.updateUI(Unknown Source)
at dvdorganizer.MyThread.run(MyThread.java:15)

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at javax.swing.plaf.basic.BasicProgressBarUI.paintDeterminate(Unknown Source)
at javax.swing.plaf.metal.MetalProgressBarUI.paintDeterminate(Unknown Source)
at javax.swing.plaf.basic.BasicProgressBarUI.paint(Unknown Source)
at javax.swing.plaf.ComponentUI.update(Unknown Source)
at javax.swing.JComponent.paintComponent(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintWithOffscreenBuffer(Unknown Source)
at javax.swing.JComponent.paintDoubleBuffered(Unknown Source)
at javax.swing.JComponent._paintImmediately(Unknown Source)
at javax.swing.JComponent.paintImmediately(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(Unknown Source)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

Pourriez-vous me dépanner svp ?