Doubt about updating JLabel in JFrame

1

Why can not I update my labelHora in Frame ? Using a print after the line labelHora.setText(x); I noticed that the content of labelHora changes, but the screen continues the default, which in this case is "zzz".

package ultilidade;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;

public class FrameInicio extends JFrame {

    private JPanel contentPane;
    public static JLabel labelHora;
    public static FrameInicio frame;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    frame = new FrameInicio();
                    DataEHora hora = new DataEHora();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public FrameInicio() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        contentPane.setLayout(new BorderLayout(0, 0));
        setContentPane(contentPane);

        labelHora = new JLabel("zzz");
        contentPane.add(labelHora, BorderLayout.NORTH);
    }
}

This class builds JFrame.

package ultilidade;

import java.text.SimpleDateFormat;
import java.util.*;


public class DataEHora extends FrameInicio{
    Date hora;
    SimpleDateFormat sdf;
    public static int i = 0;
    Timer timer;

    public DataEHora(){
        timer = new Timer();
        timer.schedule(new RemindTask(), 0, 5*1000);
    }

    public String mostraHora(){
        hora = new Date();
        sdf = new SimpleDateFormat("HH:mm");
        return sdf.format(hora);
    }

    class RemindTask extends TimerTask{

        @Override
        public void run() {
            //frame.metodo();
            //DataEHora hora = new DataEHora();
            String x = mostraHora();
            labelHora.setText(x);
            frame.repaint();
        }

    }

    /*public static void main(String[] args) {
        DataEHora d = new DataEHora();
    }*/

    /*public static void main(String[] args) {
        DataEHora deh = new DataEHora();
        deh.atualizaHora();
    } */
}

This would update.

    
asked by anonymous 02.12.2015 / 05:51

1 answer

1

It's just a detail that you missed, you instantiated the DataEHora and the FrameInicio only that you are viewing the FrameHome. Try this:

 public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                //frame = new FrameInicio(); esse objeto não eh necessário
                DataEHora hora = new DataEHora();
                hora.setVisible(true);//É esse objeto que deve ser visualizado.
                //frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

Another detail there in your time thread:

 class RemindTask extends TimerTask{

    @Override
    public void run() {
        //frame.metodo();
        //DataEHora hora = new DataEHora();
        String x = mostraHora();
        labelHora.setText(x);
        //frame.repaint(); esse repaint não eh necessário
        repaint(); //caso queira utilizar o repaint 
    }

}
    
14.07.2016 / 18:47