How to update a JLabel periodically?

1

I'd like to know how to periodically update a Jlabel , since this label depends on another method. In this case it would be 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);
    }

}
    
asked by anonymous 04.06.2015 / 20:59

1 answer

2

When working with Swing it's best to use javax.swing.Timer than java.util.Timer , this is because all < in timers of the Swing share the same thread already exists for this purpose and such thread is in dispatch thread .

To implement is very simple, just implement the interface ActionListener " which has only one method, actionPerformed . It is the one that will execute when the configured time ( delay ) ends.

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();

Here is an example of it running:

Seemoreat How to Use Swing Timers .

    
04.06.2015 / 22:38