voila qq précsions par rapport à ton premier post :
la classe ThraedWorker :
import javax.swing.SwingUtilities;
public abstract class ThreadWorker {
private Object value; // see getValue(), setValue()
private Thread thread;
/**
* Class to maintain reference to current worker thread
* under separate synchronization control.
*/
private static class ThreadVar {
private Thread thread;
ThreadVar(Thread t) { thread = t; }
synchronized Thread get() { return thread; }
synchronized void clear() { thread = null; }
}
private ThreadVar threadVar;
/**
* Get the value produced by the worker thread, or null if it
* hasn't been constructed yet.
*/
protected synchronized Object getValue() {
return value;
}
/**
* Set the value produced by worker thread
*/
private synchronized void setValue(Object x) {
value = x;
}
/**
* Compute the value to be returned by the <code>get</code> method.
*/
public abstract Object construct();
/**
* Called on the event dispatching thread (not on the worker thread)
* after the <code>construct</code> method has returned.
*/
public void finished() {
}
/**
* A new method that interrupts the worker thread. Call this method
* to force the worker to stop what it's doing.
*/
public void interrupt() {
Thread t = threadVar.get();
if (t != null) {
t.interrupt();
}
threadVar.clear();
}
/**
* Return the value created by the <code>construct</code> method.
* Returns null if either the constructing thread or the current
* thread was interrupted before a value was produced.
*
* @return the value created by the <code>construct</code> method
*/
public Object get() {
while (true) {
Thread t = threadVar.get();
if (t == null) {
return getValue();
}
try {
t.join();
}
catch (InterruptedException e) {
Thread.currentThread().interrupt(); // propagate
return null;
}
}
}
/**
* Start a thread that will call the <code>construct</code> method
* and then exit.
*/
public ThreadWorker() {
final Runnable doFinished = new Runnable() {
public void run() { finished(); }
};
Runnable doConstruct = new Runnable() {
public void run() {
try {
setValue(construct());
}
finally {
threadVar.clear();
}
SwingUtilities.invokeLater(doFinished);
}
};
Thread t = new Thread(doConstruct);
threadVar = new ThreadVar(t);
}
/**
* Start the worker thread.
*/
public void start() {
Thread t = threadVar.get();
if (t != null) {
t.start();
}
}
}
donc la méthode finished() est bien appelée apres la methode run, non?
Quote:
Si tu es dans un thread, tu ne peux normalement pas faire ça, c'est à dire APS appeler une méthode Swing qui change l'état d'un composant graphique.
ah, ok, je savais pas...
Quote:
Swing ne fontionne correctement, (si jamais il fonctionne correctement.... mais c'est un autre débat Smile ) QUE si ses méthodes sont invokées dans son propre thread, c'est à dire dans le thread principal de l'application. Sinon, dans un thread séparé, tu dois invoquer les méthodes telles que setEnabled par le biais de java.awt.EventQueue.invokeLater.
il faut que je fasse quelque chose du genre
java.awt.EventQueue.invokeLater(new Runnable){
run(){
setEnabled(true)....
}}
Il faut aussi que j'enleve threadworker .... et que je fasse plutot :
public void appelleWS{
resultingGraph = transitionGraph.exportToCADP();//appel et execution du service web
java.awt.EventQueue.invokeLater(new Runnable){
run(){
exportButton.setEnabled(true);
abortButton.setEnabled(false);
if ( txtStatus.getText().equals(RUNNING) ) {
txtStatus.setText(FINISHED);
setResultsPanel();
}
}
};
}
Est-ce que c'est ca que je dois faire?
et la, java sais qu'il faut d'abord faire "resultingGraph=..." avant de mettre à jour les boutons et tout ca?
Quote:
Si tu as ton Programmez! 74 sous la main, il y a un d'article d'initiation aux Web Service avec Java.
malheureusement, je ne l'ai pas, je me suis abonnée a partir du 80!
Merci
voila qq précsions par rapport à ton premier post :
la classe ThraedWorker :
import javax.swing.SwingUtilities; public abstract class ThreadWorker { private Object value; // see getValue(), setValue() private Thread thread; /** * Class to maintain reference to current worker thread * under separate synchronization control. */ private static class ThreadVar { private Thread thread; ThreadVar(Thread t) { thread = t; } synchronized Thread get() { return thread; } synchronized void clear() { thread = null; } } private ThreadVar threadVar; /** * Get the value produced by the worker thread, or null if it * hasn't been constructed yet. */ protected synchronized Object getValue() { return value; } /** * Set the value produced by worker thread */ private synchronized void setValue(Object x) { value = x; } /** * Compute the value to be returned by the <code>get</code> method. */ public abstract Object construct(); /** * Called on the event dispatching thread (not on the worker thread) * after the <code>construct</code> method has returned. */ public void finished() { } /** * A new method that interrupts the worker thread. Call this method * to force the worker to stop what it's doing. */ public void interrupt() { Thread t = threadVar.get(); if (t != null) { t.interrupt(); } threadVar.clear(); } /** * Return the value created by the <code>construct</code> method. * Returns null if either the constructing thread or the current * thread was interrupted before a value was produced. * * @return the value created by the <code>construct</code> method */ public Object get() { while (true) { Thread t = threadVar.get(); if (t == null) { return getValue(); } try { t.join(); } catch (InterruptedException e) { Thread.currentThread().interrupt(); // propagate return null; } } } /** * Start a thread that will call the <code>construct</code> method * and then exit. */ public ThreadWorker() { final Runnable doFinished = new Runnable() { public void run() { finished(); } }; Runnable doConstruct = new Runnable() { public void run() { try { setValue(construct()); } finally { threadVar.clear(); } SwingUtilities.invokeLater(doFinished); } }; Thread t = new Thread(doConstruct); threadVar = new ThreadVar(t); } /** * Start the worker thread. */ public void start() { Thread t = threadVar.get(); if (t != null) { t.start(); } } }donc la méthode finished() est bien appelée apres la methode run, non?
ah, ok, je savais pas...
il faut que je fasse quelque chose du genre
java.awt.EventQueue.invokeLater(new Runnable){ run(){ setEnabled(true).... }}Il faut aussi que j'enleve threadworker .... et que je fasse plutot :
public void appelleWS{ resultingGraph = transitionGraph.exportToCADP();//appel et execution du service web java.awt.EventQueue.invokeLater(new Runnable){ run(){ exportButton.setEnabled(true); abortButton.setEnabled(false); if ( txtStatus.getText().equals(RUNNING) ) { txtStatus.setText(FINISHED); setResultsPanel(); } } }; }Est-ce que c'est ca que je dois faire?
et la, java sais qu'il faut d'abord faire "resultingGraph=..." avant de mettre à jour les boutons et tout ca?
malheureusement, je ne l'ai pas, je me suis abonnée a partir du 80!
Merci