Ver Mensaje Individual
  #3 (permalink)  
Antiguo 23/07/2011, 10:20
Avatar de HackmanC
HackmanC
 
Fecha de Ingreso: enero-2008
Ubicación: Guatemala
Mensajes: 1.817
Antigüedad: 16 años, 3 meses
Puntos: 260
Sonrisa Respuesta: Problema con animación (Threads)

Hola,

Para ser mas específico puedes probar lo siguiente:

Código Java:
Ver original
  1. package org.sample;
  2.  
  3. import java.awt.event.ActionEvent;
  4. import java.awt.event.ActionListener;
  5. import java.util.EventListener;
  6.  
  7. import javax.swing.event.EventListenerList;
  8.  
  9. public class Worker extends Thread {
  10.  
  11.     private EventListenerList actionListenerList = new EventListenerList();
  12.  
  13.     @Override
  14.     public void run() {
  15.         try {
  16.             Thread.sleep(1000 * 5);
  17.             System.out.println("Working...");
  18.             Thread.sleep(1000 * 5);
  19.             fireActionPerformed(new ActionEvent(this, 1000, "FIN"));
  20.         } catch (InterruptedException e) {
  21.             System.err.println(e);
  22.         }
  23.     }
  24.  
  25.     public void addActionListener(ActionListener actionListener) {
  26.         actionListenerList.add(ActionListener.class, actionListener);
  27.     }
  28.  
  29.     public void removeActionListener(ActionListener actionListener) {
  30.         actionListenerList.remove(ActionListener.class, actionListener);
  31.     }
  32.  
  33.     protected void fireActionPerformed(ActionEvent actionEvent) {
  34.         EventListener listenerList[] = actionListenerList.getListeners(ActionListener.class);
  35.         for (int i = 0, n = listenerList.length; i < n; i++) {
  36.             ((ActionListener) listenerList[i]).actionPerformed(actionEvent);
  37.         }
  38.     }
  39.  
  40. }


Código Java:
Ver original
  1. package org.sample;
  2.  
  3. import java.awt.event.ActionEvent;
  4. import java.awt.event.ActionListener;
  5.  
  6. public class Main implements ActionListener {
  7.  
  8.     public static void main(String[] args) {
  9.         Main main = new Main();
  10.         for (int i = 0; i < 5; i++) {
  11.             Worker worker = new Worker();
  12.             worker.addActionListener(main);
  13.             worker.start();
  14.         }
  15.     }
  16.  
  17.     @Override
  18.     public void actionPerformed(ActionEvent e) {
  19.         System.out.println("El objeto " + e + " ha finalizado con ID " + e.getID() + ".");
  20.     }
  21.  
  22. }

En este caso es el main el que recibe el evento cuando se ha finalizado de ejecutar el worker, y toda la información relevante del objeto que finalizó se recibe en ActionEvent e, también puedes crear una clase que extienda ActionEvent para que contenga la información que creas necesaria.

Saludos,