Threads to show counter JAVA SWING

0

I have the next class of Thread

public class Hilo  extends Thread{

        public Hilo(String name) {
            super(name);
        }

        @Override
        public void run(){
            int cantMaxima = Sistema.TIEMPO;
            int iterador = 0 ; 
            while(iterador<cantMaxima){

            Sistema.setContador(Sistema.getContador()-1);
            iterador++;
                try {
                    sleep(1000);
                } catch (InterruptedException ex) {
                    Logger.getLogger(Hilo.class.getName()).log(Level.SEVERE, null, ex);
                }
            }

        }


    }

In my Class Controller of OneVistaX I do the following

Hilo h1 = new Hilo("Contador");
        h1.start();

now in my view I have the following

int cant = JOptionPane.showConfirmDialog("El tiempo restante es "+Sistema.getContador());

The problem that I am now with is that I do not know how to update the number in the JPanel, it remains static in the 0

Try to refresh (); and revalidete ():

I really do not know if I have it wrong or so I can solve it like showing a counter in a JPanel with a thread doing the countdown

    
asked by Bruno Sosa Fast Tag 15.11.2017 в 00:27
source

2 answers

1

Would not it be better to implement it with a timer?

I give you an example:

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.Timer;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author Eduardo Isaac Ballesteros
 */
public class TimerEjemplo2 {

    Timer timer;
    int contador = 0;
    int valorMaximo = 100;
    int valorInicial = 0;

    public static void main(String[] args) {
        TimerEjemplo2 timerEjemplo2 = new TimerEjemplo2();
    }

    public TimerEjemplo2() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                final JLabel label = new JLabel("", JLabel.CENTER);
                Hilo hilo = new Hilo("test");
                hilo.start();

                timer = new Timer(1000, new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        label.setText(String.valueOf(valorInicial));
                        valorInicial++;
                        if (valorInicial == valorMaximo) {
                            timer.stop();
                        }
                    }
                });
                timer.start();
                JOptionPane.showConfirmDialog(null, label, "Ejemplo", JOptionPane.YES_NO_OPTION, JOptionPane.PLAIN_MESSAGE);
            }
        });
    }
}

A dirty solution with Thread would be like this:

public class Sistema {
    private static int contador = 100;

    /**
     * @return the contador
     */
    public static int getContador() {
        return contador;
    }

    /**
     * @param aContador the contador to set
     */
    public static void setContador(int aContador) {
        contador = aContador;
    }
}


import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JLabel;

/**
 *
 * @author Eduardo Isaac Ballesteros
 */
public class Hilo extends Thread {

    JLabel label;

    public Hilo(JLabel pLabel) {
        this.label = pLabel;
    }

    @Override
    public void run() {
        int cantMaxima = 100;
        int iterador = 0;
        while (iterador < cantMaxima) {
            Sistema.setContador(Sistema.getContador() - 1);
            label.setText("Desde hilo: " + String.valueOf(Sistema.getContador()));
            iterador++;
            try {
                sleep(1000);
            } catch (InterruptedException ex) {
                Logger.getLogger(Hilo.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
}


import javax.swing.JLabel;
import javax.swing.JOptionPane;

/**
 *
 * @author Eduardo Isaac Ballesteros
 */
public class TimerEjemplo3 {

    public static void main(String[] args) {
        TimerEjemplo3 timerEjemplo3 = new TimerEjemplo3();
    }

    public TimerEjemplo3() {
        final JLabel label = new JLabel("", JLabel.CENTER);
        Hilo hilo = new Hilo(label);
        hilo.start();
        JOptionPane.showConfirmDialog(null, label, "Ejemplo", JOptionPane.YES_NO_OPTION, JOptionPane.PLAIN_MESSAGE);
    }

}
    
answered by 15.11.2017 / 01:17
source
0

The screen freezes because the thread is not running independently, so for the screen to refresh, I suggest the following:

new Thread(new Runnable() {

       @Override
       public void run() {
           //Colocar todos los algoritmos aca
       }
   }).start();
    
answered by 29.11.2017 в 02:47