How to update a Jlabel periodically?

Asked

Viewed 1,884 times

1

I would like to know how to keep updating periodically a Jlabel, since this label depends on another method. In this case it would be the lblNewLabel_1.

public class MenuGFinancas extends JFrame {

    private JPanel contentPane1;
    private String nome;
    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    MenuGFinancas frame = new MenuGFinancas("Teste");
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public MenuGFinancas(String nome) {

        ImageIcon image = new ImageIcon("C:\\Users\\admin\\Documents\\Java         Project PP\\Imagens\\icone_prestacao_contas.jpg");

        setTitle("GFinancas Beta");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 770, 427);
        setIconImage(image.getImage());
        setResizable(false);
        setLocationRelativeTo(null);

        contentPane1 = new JPanel();
        contentPane1.setForeground(new Color(152, 251, 152));
        contentPane1.setOpaque(true);
        contentPane1.setBackground(new Color(255, 69, 0));
        contentPane1.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane1);
        contentPane1.setLayout(null);
        setVisible(true);

        MenuBar menu = new MenuBar();
        menu.setBackground(Color.CYAN);
        menu.setBounds(0, 0, 764, 22);
        contentPane1.add(menu);
        menu.setLayout(new CardLayout(0, 0));

        JLabel lblNewLabel = new JLabel("Olá " +nome);
        lblNewLabel.setForeground(Color.WHITE);
        lblNewLabel.setFont(new Font("Trajan Pro", Font.BOLD, 14));
        lblNewLabel.setBounds(0, 22, 764, 34);
        contentPane1.add(lblNewLabel);

        JLabel lblContaCorrente = new JLabel("Conta Corrente");
        lblContaCorrente.setForeground(Color.ORANGE);
        lblContaCorrente.setFont(new Font("Tahoma", Font.BOLD, 18));
        lblContaCorrente.setBounds(36, 113, 174, 22);
        contentPane1.add(lblContaCorrente);

        JLabel lblSaldo = new JLabel("Saldo: ");
        lblSaldo.setForeground(Color.WHITE);
        lblSaldo.setFont(new Font("Tahoma", Font.BOLD, 14));
        lblSaldo.setBounds(36, 165, 61, 14);
        contentPane1.add(lblSaldo);

        MostraSaldo mostraSaldo = new MostraSaldo();
        DecimalFormat df = new DecimalFormat("0.00");
        String saldo = String.valueOf(df.format(mostraSaldo.PegaSaldo()));
        JLabel lblNewLabel_1 = new JLabel(saldo);
        lblNewLabel_1.setFont(new Font("Tahoma", Font.BOLD, 14));
        lblNewLabel_1.setBounds(107, 165, 68, 14);
        contentPane1.add(lblNewLabel_1);



        JLabel label = new JLabel("");
        label.setBackground(new Color(153, 255, 51));
        label.setIcon(new ImageIcon("C:\\Users\\admin\\Documents\\Java Project PP\\Imagens\\back.png"));
        label.setBounds(0, 0, 764, 399);
        contentPane1.add(label);
        setVisible(true);
    }

}
  • No friend, this label receives a value from the Database, I wanted it to update, for example, 15 in 15sec, in case some value is added to the Database, the value appears in this label. I don’t know if I’m talking nonsense, I’m new, but a priori my idea was this.

  • I get it, I guess that answer of utluiz can help.

  • To update I think it is lblNewLabel_1.repaint();

1 answer

2


When working with Swing it is more recommended to use javax.swing.Timer than java.util.Timer, this why all the timers of Swing share the same thread already existing for this purpose and such thread is in the Event Dispatch thread .

To implement it is quite simple, just implement the interface ActionListener which has only one method, actionPerformed. It is he who will run when the time set (delay) end up.

An example of timer to update your UI would look like this:

private class TimerToLabel implements ActionListener {

    private Timer timer;
    private final JLabel label;
    private final int delay;

    public TimerToLabel(final int delay, final JLabel label) {
        this.delay = delay;
        this.label = label;
    }

    public void init() {
        timer = new Timer(delay, this);
        timer.start();
    }

    @Override
    public void actionPerformed(final ActionEvent e) {
        // faça aqui sua consulta
        label.setText("" + Math.random());
        label.updateUI();
    }

}

Considering your label, lblNewLabel_1, just include this:

// configurado para 1 segundo, configure conforme sua necessidade (em milisegundos)
final TimerToLabel timer = new TimerToLabel(1000, lblNewLabel_1);
timer.init();

This is an example of him running:

Timer rodando

See more in How to Use Swing Timers.

Browser other questions tagged

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