SwingWorker et JProgressBar
Bonjour à tous,
j'essaye d'utiliser SwingWorker pour mettre à jour la valeur d'une barre de progression pendant un traitement mais rien ne bouge.
Le traitement est lancé à l'aide d'un bouton sur l'écran (classe Ecran.java).
Voici mon code :
La classe représentant mon écran
Code:
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
|
public class Ecran extends JFrame
public Ecran()
{
barreProgression = new JProgressBar();
barreProgression.setBounds(20, 113, 403, 25);
barreProgression.setVisible(false);
barreProgression.setStringPainted(true);
panel.add(barreProgression);
bouton = new BJButton();
bouton.setAction(getActionBouton());
this.getPanelBas().addButton(bouton);
}
private Action getActionBouton()
{
Action actionSelection = new AbstractAction()
{
public void actionPerformed(ActionEvent e)
{
setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
int retourValConfirmationInj = controller.confirmerTraitement();
if(retourValConfirmationInj == JOptionPane.OK_OPTION)
{
afficherBarreProgression();
controller.actionBouton();
}
}
};
return actionSelection;
}
public void afficherBarreProgression()
{
barreProgression.setVisible(true);
} |
La classe liée à mon controller
Code:
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
|
public class Controller
{
public JFrame getEcran()
{
ecran = new Ecran();
return ecran;
}
public void actionBouton()
{
SwingWorker sw = new SwingWorker()
{
protected Object doInBackground() throws Exception
{
while(rowIterator.hasNext())
{
setProgress(pourcentageProgress);
}
}
};
sw.addPropertyChangeListener(new PropertyChangeListener()
{
public void propertyChange(PropertyChangeEvent evt)
{
if ("progress".equals(evt.getPropertyName()))
{
ecran.getBarreProgression().setValue((Integer) evt.getNewValue());
if((Integer)evt.getNewValue() == 100)
{
JOptionPane.showMessageDialog(getView(), "FIN", "", JOptionPane.INFORMATION_MESSAGE);
}
}
}
});
}
} |
Le message "FIN" dans mon JOptionPane s'affiche correctement mais la valeur de ma progressbar ne s'affiche pas.
Je n'arrive pas à comprendre pourquoi la valeur ne se met pas à jour. Est-cu du au fait que mon ecran n'est pas construit dans l'EDT?
Merci d'avance