Set JLabel dynamic text to JDialog by timer

I am trying to make a JDialog that will show the user a dynamic message in JLabel. The message should be a number from 1 to 10 (and it should change the number of times per second). the fact is that when im debugs it - it stops right after "dia.setVisible (true)" ;, and will not continue if I do not close JDialog. Is there any possible way to fix it? Thank.

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

}
+1
source share
4 answers
  • do not use Thread.sleep(int)for Swing GUI, caused freeze until Thread.sleep(int)end

  • use Swing Timerinstead of locking Swing GUI withThread.sleep(int)

  • dia.setSize(300, 150), , LayoutManager

+3

setVisible JDialog. Runnable. Runnable.run() .

+2

Take a look at this sample code for the correct way to use dynamic text using 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();
            }
        });
    }
}
+2
source

make sure it jlis 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);
0
source

All Articles