Trabalhando com javax.swing.Timer?

galera,seguinte.

Eu tenho um JLabel que precisa ir exibindo a hora por segundos, estou tentando usar o javax.swing.Timer para isso com NetBeans6

meu Codigo e:

 public void iniciaHoraCorrente(){
        String horaCorrenteSistema = "";
        SimpleDateFormat horaFormatada = new SimpleDateFormat("HH:mm:ss");
        Date horaAtual = new Date();
        horaCorrenteSistema = horaFormatada.format(horaAtual);
        horaCorrente.setVisible(true);
        ActionListener action = new ActionListener() {
            public void actionPerformed(ActionEvent e) {
               horaCorrente.setText(horaCorrenteSistema);
               
            }
        };
        Timer timer = new Timer(1000, action);
        timer.start();        
        
    }

Isso não esta funcionando, o JLabel horaCorrente não inicia o contador do timer…é dessa maneira msm que se faz ???
Eu ouvi falar de um componente chamado TimerBean para fazer isso, mas não quero usar esse componente se conseguir fazer com que funcione o javax.swing.Timer.

Obrigado

Aqui tem um exemplo com threads: http://www.guj.com.br/posts/list/52964.java#279083

Na página seguinte, com o java.util.Timer e em seguida com o javax.swing.Timer

Boa diversão. :slight_smile:

        ActionListener action = new ActionListener() {  
            public void actionPerformed(ActionEvent e) {  
               horaCorrente.setText(horaCorrenteSistema);  
                 
            }  
        };

Você só mexeu nessa variável uma única vez. Quando esse método for chamado pela 2a, 3a, … vezes, ele vai sempre mostrar o mesmo valor, porque o valor da variável não se mexeu.

opa…consegui fazer o esquema…kkkkk !! :smiley:

fiz assim.


public class ExibeRelogio extends javax.swing.JFrame {

    private static final DateFormat FORMATO = new SimpleDateFormat("HH:mm:ss");
    /** Creates new form ExibeRelogio */
    public ExibeRelogio() {
        initComponents();
        iniciaRelogio();

    }

    public void setHora(Date data){
        relogio.setText(FORMATO.format(data));
    }

    public void iniciaRelogio(){
        ActionListener action = new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                setHora(new Date());
            }
        };
        javax.swing.Timer timer = new javax.swing.Timer(1000, action);
        timer.setInitialDelay(0);
        timer.setRepeats(true);
        timer.start();
    }

ta funcionando aqui, ficou rox…vou brincar pra ver o q da pra fazer, qquer duvida eu posto ae.

obrigado a todos…vcs sao feras :wink: