Question about updating Jlabel in Jframe

Asked

Viewed 150 times

1

Why can’t I update my labelHora in the Frame? Using a print after the line labelHora.setText(x); I realized that the content of lip changes, but on the screen continues the pattern, which in the 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();
    } */
}

That would update.

  • Are you using null layout?

  • In this case is the border. My doubt arose in a project that I am developing, so the code does not get too extensive I created these two classes.

  • when you run this run() and when you run the code that creates Jlabel?

1 answer

1

It’s just a detail that you missed, you prompted Dataehora and Frameinicio only that you are viewing Frameinicio. Try this way:

 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 thread of the hour:

 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 
    }

}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.