Mi SwingWorker no se cancela

Iniciado por Zoik, 29 Agosto 2013, 10:07 AM

0 Miembros y 1 Visitante están viendo este tema.

Zoik

#10
Bueno pues te dejo el código aqui a ver si a ti te funciona, yo ya estoy perdiendo los nervios.

Código (java) [Seleccionar]
package base;

import gui.Gui;

import javax.swing.SwingUtilities;

public class Main {

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(){
public void run(){
new Gui();
}
});
}
}


Código (java) [Seleccionar]
package gui;

import java.awt.Component;
import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Time;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;

import base.CheckTime;

public class Gui extends JFrame implements ActionListener{
private int width = 370,height = 150;
private String windowTitle = "ShutdownProgramer";
private String textButtonProgram = "Program shutdown";
private String textButtonCancel = "Cancel shutdown";
private int maxHour = 24,maxMin = 60,maxSec = 60;
private JComboBox<Integer> hour = new JComboBox<Integer>();
private JComboBox<Integer> min = new JComboBox<Integer>();
private JComboBox<Integer> sec = new JComboBox<Integer>();
private JButton programShutdown = new JButton(textButtonProgram);
private JButton cancelShutdown = new JButton(textButtonCancel);
private boolean isStarted = false;
public Gui(){
setSize(width,height);
setTitle(windowTitle);
setLocationRelativeTo(null);
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new GridBagLayout());
configureJComboBoxes();
configureJButton();
setVisible(true);
}

public void addComponentGBL(Container container, Component component, int gridx, int gridy, int gridwidth, int gridheight, int anchor, int fill, int inset1, int inset2, int inset3, int inset4) {
Insets insets = new Insets(inset1, inset2, inset3, inset4);
GridBagConstraints gbc = new GridBagConstraints(gridx, gridy, gridwidth, gridheight, 1.0, 1.0, anchor, fill, insets, 0, 0);
   container.add(component, gbc);
 }

public void configureJComboBoxes(){
for(int i = 0; i < maxHour; i++)
hour.addItem(i);
for(int i = 0; i < maxMin; i++)
min.addItem(i);
for(int i = 0; i < maxSec; i++)
sec.addItem(i);

addComponentGBL(this,hour,0,0,1,0,GridBagConstraints.CENTER, GridBagConstraints.CENTER, 0, 90, 40, 0);
addComponentGBL(this,min,1,0,1,0,GridBagConstraints.CENTER, GridBagConstraints.CENTER, 0, 0, 40, 0);
addComponentGBL(this,sec,2,0,1,0,GridBagConstraints.CENTER, GridBagConstraints.CENTER, 0, 0, 40, 90);
}

public void configureJButton(){
addComponentGBL(this,programShutdown,0,0,0,0,GridBagConstraints.CENTER, GridBagConstraints.CENTER, 40, 0, 0, 150);
addComponentGBL(this,cancelShutdown,0,0,0,0,GridBagConstraints.CENTER, GridBagConstraints.CENTER, 40, 150, 0, 0);
programShutdown.addActionListener(this);
cancelShutdown.addActionListener(this);
}

   public void actionPerformed(ActionEvent evt) {
   CheckTime time = new CheckTime(hour.getSelectedIndex(), min.getSelectedIndex(), sec.getSelectedIndex());
   if(evt.getSource() == programShutdown && isStarted == false){
   System.out.println("iniciado!");
   time.execute();
   isStarted = true;
   } else if(evt.getSource() == cancelShutdown && isStarted == true){
   System.out.println("cancelado!");
   time.stopThread();
   isStarted = false;
   }
   
   }

}


Código (java) [Seleccionar]
package base;

import java.util.Calendar;

import javax.swing.SwingWorker;

public class CheckTime extends SwingWorker<Object, Object> {

private int copyHour;
private int copyMin;
private int copySec;
private boolean running;

public CheckTime(int hour, int min, int sec) {
copyHour = hour;
copyMin = min;
copySec = sec;
}

protected Object doInBackground() throws Exception {
Calendar calendario;

int thisHour;
int thisMin;
int thisSec;

this.running = true;

System.out.println("iniciado!");

do {
calendario = Calendar.getInstance();
thisHour = calendario.get(Calendar.HOUR_OF_DAY);
thisMin = calendario.get(Calendar.MINUTE);
thisSec = calendario.get(Calendar.SECOND);

System.out.println("funcionando: " + thisHour + ":" + thisMin + ":" + thisSec);

if (copyHour == thisHour && copyMin == thisMin && copySec == thisSec) {
this.running = false;
}

try {
Thread.sleep(1000);
} catch (InterruptedException e) {
System.out.println("Thread sleep de ChekcHour a fallado");
}
} while (this.running);

System.out.println("APAGATE!");
return null;
}

public void stopThread() {
this.running = false;
}
}


Un saludo.

Fakedo0r

A ver no entiendo, te di el código hecho pero vuelves a poner mal ?????? Copia el código que te di amigo. El último código.
Te explique y te di mil ejemplos. Ahora ponte a mirarlo. Antes de aprender a cabalgar, primero hace falta aprender a caminar.

Saludos.

Zoik

Bueno con el cambio que has echo en el anterior post, si que funciona cuando coinciden las dos horas, el cancelar, sigue como siempre, pero ya lo mirare con mas calma, quien quiera probar el código copy and paste.

Un saludo y gracias.