Search code examples
loopsjlabeljdialogsettext

Making JDialog have continuoulsly changing Text Like "working..."


I have a simple JDialog with a progress bar and I want it to have a JLabel that goes like:

Working. --> Working.. --> Working...

about every .25 seconds.

Would I just run a continuous loop repeatedly setting the JLabel's text?

I know this must be a simple solution, but I cannot seem to find it anywhere!

Per @David's response below. Here is the bulk of my WorkingDialog (which extends JDialog) constructor:

    final JLabel lblWorking = new JLabel("Working.");
    contentPanel.add(lblWorking, gbc_lblWorking);

    SwingWorker<Void, Void> sw = new SwingWorker<Void, Void>()
    {

        @Override
        protected Void doInBackground() throws Exception
        {
            while (!isCancelled())
            {
                if (!lblWorking.getText().equals("Working..."))
                {
                    lblWorking.setText("Working.");
                } else
                {
                    lblWorking.setText(lblWorking.getText() + ".");
                }
            }

            return null;
        }
    };

    setVisible(true);
    sw.run();

And when I call new WorkingDialog() the frame comes up but thats about it. Does anyone know why?

Thanks,

Rich


Solution

  • Create a daemon that will update your label every 0.25 seconds. Dont forget to use SwingUtils.invokeLater() when updating the label.

    I guess you should be able to do it with SwingWorker too.

    Cheers