elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: Usando Git para manipular el directorio de trabajo, el índice y commits (segunda parte)


+  Foro de elhacker.net
|-+  Programación
| |-+  Programación General
| | |-+  Java
| | | |-+  Mi SwingWorker no se cancela
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: 1 [2] Ir Abajo Respuesta Imprimir
Autor Tema: Mi SwingWorker no se cancela  (Leído 5,266 veces)
Zoik

Desconectado Desconectado

Mensajes: 91


Ver Perfil
Re: Mi SwingWorker no se cancela
« Respuesta #10 en: 29 Agosto 2013, 16:33 pm »

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

Código
  1. package base;
  2.  
  3. import gui.Gui;
  4.  
  5. import javax.swing.SwingUtilities;
  6.  
  7. public class Main {
  8.  
  9. public static void main(String[] args) {
  10. SwingUtilities.invokeLater(new Runnable(){
  11. public void run(){
  12. new Gui();
  13. }
  14. });
  15. }
  16. }
  17.  

Código
  1. package gui;
  2.  
  3. import java.awt.Component;
  4. import java.awt.Container;
  5. import java.awt.GridBagConstraints;
  6. import java.awt.GridBagLayout;
  7. import java.awt.Insets;
  8. import java.awt.event.ActionEvent;
  9. import java.awt.event.ActionListener;
  10. import java.sql.Time;
  11.  
  12. import javax.swing.JButton;
  13. import javax.swing.JComboBox;
  14. import javax.swing.JFrame;
  15.  
  16. import base.CheckTime;
  17.  
  18. public class Gui extends JFrame implements ActionListener{
  19. private int width = 370,height = 150;
  20. private String windowTitle = "ShutdownProgramer";
  21. private String textButtonProgram = "Program shutdown";
  22. private String textButtonCancel = "Cancel shutdown";
  23. private int maxHour = 24,maxMin = 60,maxSec = 60;
  24. private JComboBox<Integer> hour = new JComboBox<Integer>();
  25. private JComboBox<Integer> min = new JComboBox<Integer>();
  26. private JComboBox<Integer> sec = new JComboBox<Integer>();
  27. private JButton programShutdown = new JButton(textButtonProgram);
  28. private JButton cancelShutdown = new JButton(textButtonCancel);
  29. private boolean isStarted = false;
  30. public Gui(){
  31. setSize(width,height);
  32. setTitle(windowTitle);
  33. setLocationRelativeTo(null);
  34. setResizable(false);
  35. setDefaultCloseOperation(EXIT_ON_CLOSE);
  36. setLayout(new GridBagLayout());
  37. configureJComboBoxes();
  38. configureJButton();
  39. setVisible(true);
  40. }
  41.  
  42. 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) {
  43. Insets insets = new Insets(inset1, inset2, inset3, inset4);
  44. GridBagConstraints gbc = new GridBagConstraints(gridx, gridy, gridwidth, gridheight, 1.0, 1.0, anchor, fill, insets, 0, 0);
  45.    container.add(component, gbc);
  46.  }
  47.  
  48. public void configureJComboBoxes(){
  49. for(int i = 0; i < maxHour; i++)
  50. hour.addItem(i);
  51. for(int i = 0; i < maxMin; i++)
  52. min.addItem(i);
  53. for(int i = 0; i < maxSec; i++)
  54. sec.addItem(i);
  55.  
  56. addComponentGBL(this,hour,0,0,1,0,GridBagConstraints.CENTER, GridBagConstraints.CENTER, 0, 90, 40, 0);
  57. addComponentGBL(this,min,1,0,1,0,GridBagConstraints.CENTER, GridBagConstraints.CENTER, 0, 0, 40, 0);
  58. addComponentGBL(this,sec,2,0,1,0,GridBagConstraints.CENTER, GridBagConstraints.CENTER, 0, 0, 40, 90);
  59. }
  60.  
  61. public void configureJButton(){
  62. addComponentGBL(this,programShutdown,0,0,0,0,GridBagConstraints.CENTER, GridBagConstraints.CENTER, 40, 0, 0, 150);
  63. addComponentGBL(this,cancelShutdown,0,0,0,0,GridBagConstraints.CENTER, GridBagConstraints.CENTER, 40, 150, 0, 0);
  64. programShutdown.addActionListener(this);
  65. cancelShutdown.addActionListener(this);
  66. }
  67.  
  68.    public void actionPerformed(ActionEvent evt) {
  69.    CheckTime time = new CheckTime(hour.getSelectedIndex(), min.getSelectedIndex(), sec.getSelectedIndex());
  70.    if(evt.getSource() == programShutdown && isStarted == false){
  71.    System.out.println("iniciado!");
  72.    time.execute();
  73.    isStarted = true;
  74.    } else if(evt.getSource() == cancelShutdown && isStarted == true){
  75.    System.out.println("cancelado!");
  76.    time.stopThread();
  77.    isStarted = false;
  78.    }
  79.  
  80.    }
  81.  
  82. }
  83.  

Código
  1. package base;
  2.  
  3. import java.util.Calendar;
  4.  
  5. import javax.swing.SwingWorker;
  6.  
  7. public class CheckTime extends SwingWorker<Object, Object> {
  8.  
  9. private int copyHour;
  10. private int copyMin;
  11. private int copySec;
  12. private boolean running;
  13.  
  14. public CheckTime(int hour, int min, int sec) {
  15. copyHour = hour;
  16. copyMin = min;
  17. copySec = sec;
  18. }
  19.  
  20. protected Object doInBackground() throws Exception {
  21. Calendar calendario;
  22.  
  23. int thisHour;
  24. int thisMin;
  25. int thisSec;
  26.  
  27. this.running = true;
  28.  
  29. System.out.println("iniciado!");
  30.  
  31. do {
  32. calendario = Calendar.getInstance();
  33. thisHour = calendario.get(Calendar.HOUR_OF_DAY);
  34. thisMin = calendario.get(Calendar.MINUTE);
  35. thisSec = calendario.get(Calendar.SECOND);
  36.  
  37. System.out.println("funcionando: " + thisHour + ":" + thisMin + ":" + thisSec);
  38.  
  39. if (copyHour == thisHour && copyMin == thisMin && copySec == thisSec) {
  40. this.running = false;
  41. }
  42.  
  43. try {
  44. Thread.sleep(1000);
  45. } catch (InterruptedException e) {
  46. System.out.println("Thread sleep de ChekcHour a fallado");
  47. }
  48. } while (this.running);
  49.  
  50. System.out.println("APAGATE!");
  51. return null;
  52. }
  53.  
  54. public void stopThread() {
  55. this.running = false;
  56. }
  57. }

Un saludo.


« Última modificación: 29 Agosto 2013, 16:54 pm por Zoik » En línea

Fakedo0r

Desconectado Desconectado

Mensajes: 21


Fuera de compás, en una de las 12 dimensiones...


Ver Perfil WWW
Re: Mi SwingWorker no se cancela
« Respuesta #11 en: 29 Agosto 2013, 16:37 pm »

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.


En línea

Zoik

Desconectado Desconectado

Mensajes: 91


Ver Perfil
Re: Mi SwingWorker no se cancela
« Respuesta #12 en: 29 Agosto 2013, 16:58 pm »

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.
En línea

Páginas: 1 [2] Ir Arriba Respuesta Imprimir 

Ir a:  

WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines