Bonjour à tous,

Je suis en train de créer une classe de téléchargement.
Je me suis inspirer de la classe SwingWorker, car j'ai des problèmes d'affichage lié a l'EDT.
En effet dès que je souhaite attendre la fin du téléchargement pour être sur que le téléchargement est terminé et réussi. j'ai le JDialog qui n'affiche plus son contenu:

Nom : 389199.jpg
Affichages : 55
Taille : 7,8 Ko

Voici la classe reconstruite dans une seule classe pour faire des essais chez vousTéléchargement d'un document pdf)
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
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
 
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
 
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.SwingWorker;
 
public class SwingWorkerDemo extends JFrame {
 
	/**
         * 
         */
	private static final long serialVersionUID = 1L;
	private JTextField installe= new JTextField();
	private JDialog dialog;
	private boolean telechargement_fini=false;
	private boolean cancel=false;
 
    class MonSwingWorker extends SwingWorker<Integer, String> {
 
    	private URL url;
    	private String localFilename;
    	private String userAgent;
 
		public MonSwingWorker(URL url, String localFilename, String userAgent){
			this.url=url;
    		this.localFilename=localFilename;
    		this.userAgent=userAgent;
        }
		public Integer Calcul() throws IOException {
			InputStream is = null;
    		FileOutputStream fos = null;
    		Boolean erreur=false;
    		installe.setText("Chargement (0%)");
    		try {
    			URLConnection urlConn = url.openConnection();
 
    			int fileLength = urlConn.getContentLength();
    			if (fileLength == -1)
    			{
    				System.out.println("An error occured !");
    				erreur=true;
    			}
    			if(!erreur){
    				if (userAgent != null) {
    					urlConn.setRequestProperty("User-Agent", userAgent);
    				}
 
    				is = urlConn.getInputStream();
    				fos = new FileOutputStream(localFilename);
 
    				byte[] buffer = new byte[4096];
    				int len, percent, rate = 0;
 
    				while ((len = is.read(buffer)) > 0 && !cancel) {
    					fos.write(buffer, 0, len);
    					rate += len;
    					percent = (int)(rate * 100 / fileLength);
    					installe.setText("Chargement (" + percent + "%)");
    				}
    			}
    		} finally {
    			try {
    				if (is != null) {
    					is.close();
    				}
    			} finally {
    				if (fos != null) {
    					fos.close();
    				}
    			}
    		}
    		if(!erreur){
    			installe.setText("Chargement (100%)");
    			//dialog.setVisible(false);
    			cancel=false;
    			telechargement_fini=true;
    		}
			return null;
		}
 
        @Override
        public Integer doInBackground() {
        	Integer calcul = null;
			try {
				calcul = Calcul();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
            return calcul;
        }
		public boolean isTelechargement_fini() {
			// TODO Auto-generated method stub
			return telechargement_fini;
		}
    }
 
    public SwingWorkerDemo() {
        /* Construction de l'interface graphique. */
        super("SwingWorkerDemo");
		installe.setEditable(false);
		Object[] array = {"Téléchargement en cours...", installe};
		String[] options={"Annuler"};
		final JOptionPane optionpane = new JOptionPane(array, JOptionPane.INFORMATION_MESSAGE, JOptionPane.CANCEL_OPTION, null , options);
		dialog = new JDialog();
		dialog.setContentPane(optionpane);
		dialog.setLocationRelativeTo(null);
		dialog.pack();
		dialog.setVisible(true);
		optionpane.addPropertyChangeListener(new PropertyChangeListener() {
			public void propertyChange(PropertyChangeEvent e) {
				String value = ((String)optionpane.getValue()).intern();
				System.out.println("option pane"+value);
				if (dialog.isVisible() && (e.getSource() == optionpane) && value.equals("Annuler")) {
					cancel=true;
					dialog.setVisible(false);
				}
			}
		});
    }
 
    public static void main(String... args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                /* Démarrage de l'interface graphique et du SwingWorker. */
                SwingWorkerDemo demo = new SwingWorkerDemo();
                MonSwingWorker swingWorker = null;
				try {
					swingWorker = demo.new MonSwingWorker(new URL("http://jep2006.irisa.fr/word-to-pdf.pdf"), "Guide_pdf.pdf", null);
				} catch (MalformedURLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
                swingWorker.execute();
                // On attend la fin du téléchargement
                while(swingWorker.isDone()==false) {
                    // ne rien faire
                }
                //Action après le téléchargement....
                if(swingWorker.isTelechargement_fini()){
        			JOptionPane.showMessageDialog(null, "Le téléchargement est terminé", "Téléchargement terminé", JOptionPane.INFORMATION_MESSAGE);
        		}
            }
        });
    }
}
Le problème d'affichage est lié au :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
 while(swingWorker.isDone()==false) {
                    // ne rien faire
                }
En effet si on supprime ce code, tous fonctionne sauf qu'on attend pas la fin du téléchargement!

Merci pour votre aide