Oct 26
SwingWorker e interfacce swing che non si bloccano
java No Comments »
Buy Motilium Online
Hoodia Without Prescription
Topamax Online
Buy Prozac
Aldactone Online
Buy Toprol XL
Stromectol Online
Buy Amoxil
Glucotrol Online
Buy Stromectol
Molti programmatori java si saranno scontrati con il problema di vedere le loro interfacce java bloccarsi su task lunghi da eseguire anche se gestiti con la tecnica del multithreading. La sun ha rimediato al problema grazie alla classe SwingWorker Swingworker può essere invocato da un thread iniziale. Il metodo di questa classe “doInBackground”, lavora in un separato thread, finchè non ritorna al thread che lo ha invocato non appena finisce la sua esecuzione. Il metodo done, eseguito nell’event dispatch thread, invoca gets per restituire il controllo a chi l’ha invocatoQuesto un esempio tratto dal sito della sun
SwingWorker worker = new SwingWorker() {
@Override
public ImageIcon[] doInBackground() {
final ImageIcon[] innerImgs = new ImageIcon[nimgs];
for (int i = 0; i < nimgs; i++) {
innerImgs[i] = loadImage(i+1);
}
return innerImgs;
}
@Override
public void done() {
//Remove the "Loading images" label.
animator.removeAll();
loopslot = -1;
try {
imgs = get();
} catch (InterruptedException ignore) {}
catch (java.util.concurrent.ExecutionException e) {
String why = null;
Throwable cause = e.getCause();
if (cause != null) {
why = cause.getMessage();
} else {
why = e.getMessage();
}
System.err.println("Error retrieving file: " + why);
}
}
};