[SwingWorker] tâche longue interruptible
Bonjour à tous,
je viens de lire et comprendre le tuto sur SwingWorker (http://rom.developpez.com/java-swingworker/), mais je n'arrive pas à l'adapter à mon cas.
Voici ce que je voudrais faire :
J'ai une Frame avec un bouton "Start".
Lorsque je clique sur ce bouton, j'ouvre une boite de Dialog du style "veuillez patienter" avec une barre de progression et un bouton "Cancel", et en parallèle j'exécute un traitement qui dure assez longtemps.
Seulement voilà, avec le code que j'ai écrit, la boîte de Dialog s'ouvre mais est "freezée", et ce pendant toute la durée du traitement qui tourne en parallèle, puis le traitement se termine et elle se "dé-freeze".
Ce qui n'est évidemment pas le but recherché.
Je vais mettre mon code ci-dessous, si quelqu'un peut me dire où je fais fausse route, ça m'aiderait.
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 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 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
|
public class MainFrame extends JFrame
{
private SwingWorker testSW;
private SettingsPanel settingsPanel;
private WaitDialog waitDialog;
/** Creates a new instance of MainFrame */
public MainFrame ()
{
initComponents();
}
private void initComponents()
{
super ("Test SwingWorker");
this.setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
settingsPanel = new SettingsPanel();
settingsPanel.getStartButton ().addActionListener (new ActionListener() { public void actionPerformed(ActionEvent e) { startClicked (); } });
// WaitDialog() extends JDialog
waitDialog = new WaitDialog(this, false);
waitDialog.setResizable (false);
waitDialog.setDefaultCloseOperation (DO_NOTHING_ON_CLOSE);
waitDialog.addWindowListener (new WindowAdapter() { public void windowClosing(WindowEvent e) { cancelProcess (); } } );
waitDialog.setSize (300, 150);
waitDialog.setMinimumSize (waitDialog.getSize ());
waitDialog.getMessage().setText ("Process running...");
waitDialog.getProgressBar ().setIndeterminate (true);
waitDialog.getCancelButton ().addActionListener (new ActionListener() { public void actionPerformed(ActionEvent e) { cancelProcess (); } });
showSettingsPanel (true);
}
public void showSettingsPanel(boolean b)
{
if (b)
{
this.add(settingsPanel);
this.pack();
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
this.setLocation((screenSize.width - this.getWidth ()) / 2,
(screenSize.height - this.getHeight()) / 2);
}
else
{
this.remove (settingsPanel);
this.setMinimumSize (new Dimension());
this.pack ();
}
}
public void showWaitDialog(boolean b)
{
if (b)
{
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
waitDialog.setLocation((screenSize.width - waitDialog.getWidth ()) / 2,
(screenSize.height - waitDialog.getHeight()) / 2);
waitDialog.setVisible(true);
}
else
{
waitDialog.setVisible(false);
}
}
private void startClicked()
{
// Show wait Dialog
showWaitDialog (true);
testSW = new TestSwingWorker();
testSW.execute ();
try{ System.out.println((String)compareSW.get ()); }
catch (InterruptedException e) {System.out.println("interrompu");}
catch (Exception e) {System.out.println("erreur");}
}
private void cancelProcess ()
{
System.out.println("Process cancelled");
showWaitDialog (false);
testSW.cancel (true);
}
public static void start()
{
EventQueue.invokeLater (new Runnable()
{
public void run ()
{
new MainFrame().setVisible(true);
}
});
}
class TestSwingWorker extends SwingWorker <String, String>
{
public TestSwingWorker () { super(); }
protected String doInBackground()
{
System.out.println("[DEB] doInBackgroung()");
// juste un test
for (int i = 0; i < 5; i++)
try { Thread.sleep (1000); } catch (InterruptedException e) {}
return "[FIN] doInBackground()";
}
protected void done()
{
System.out.println("Terminated");
}
}
} |