简体   繁体   中英

Set dynamic JLabel text in a JDialog by timer

Im trying to make a JDialog that will show the user a dynamic message on a JLabel. The message should be a count from 1 to 10 (and it should be changing a number every second). the thing is , when im debugging it - it's stops right after the "dia.setVisible(true);" , and will not proceed unless i will close the JDialog . is there any possible way of fixing it? Thanks.

Take a look at the code :

    @Override
public void run() {

    dia = new JDialog(parentDialog, true);
    dia.setLocationRelativeTo(parentFrame);


    String text = "Text ";
    dia.setSize(300, 150);
    jl = new JLabel(text);
    dia.getContentPane().add(jl);
    dia.setVisible(true);
    for (int i = 0; i < 10; i++) {
        try {
            Thread.sleep(1000);
            jl.setText(text + " " + i);

        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}
  • don't use Thread.sleep(int) for Swing GUI, caused freeze of untill Thread.sleep(int) ended

  • use Swing Timer instead of locking Swing GUI by usage of Thread.sleep(int)

  • don't use dia.setSize(300, 150) , learn how LayoutManager works

setVisible is a blocking call on JDialog's. You should start an other Thread and pass a Runnable to it. The Runnable.run() method should contain your loop.

Have a look at this code example, that's the proper way to use dynamic text with the help of javax.swing.Timer Tutorials , instead of using Thread.sleep(...) thingy,

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class DialogExample extends JDialog
{
    private Timer timer;
    private JLabel changingLabel;
    private int count = 0;
    private String initialText = "TEXT";

    private ActionListener timerAction = new ActionListener()
    {
        public void actionPerformed(ActionEvent ae)
        {
            count++;
            if (count == 10)
                timer.stop();
            changingLabel.setText(initialText + count); 
        }
    };

    private void createDialog()
    {
        setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        setLocationByPlatform(true);

        JPanel contentPane = new JPanel();
        changingLabel = new JLabel(initialText);
        contentPane.add(changingLabel);

        add(contentPane);

        pack();
        setVisible(true);
        timer = new Timer(1000, timerAction);
        timer.start();
    }

    public static void main(String... args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                new DialogExample().createDialog();
            }
        });
    }
}

make sure jl is defined as final :

...
dia.getContentPane().add(jl);

new Thread(new Runnable() {
    for (int i = 0; i < 10; i++) {
        try {
            Thread.sleep(1000);
            jl.setText(text + " " + i);

        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}).run();

dia.setVisible(true);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM