1

I have a method that updates part of the user interface. After this method is called I want the entire program to sleep for say 1 second. I do not want to run any code during this time, just simply pause the entire execution. What would be the best way of achieving this?

My reason is this, I am updating the GUI quite a bit and I want the user to see the change before the next change is made.

James Fazio
  • 6,370
  • 9
  • 38
  • 47
  • Any specific reason to do that? You can make the current thread sleep by using Thread.currentThread().sleep(), but other threads would need to get a signal for them also to sleep, if that's what you intend to do. – Vikdor Oct 30 '12 at 02:52
  • What do you want the GUI to do while your program is sleeping? Should it be frozen? – Taymon Oct 30 '12 at 02:52
  • Updated with reason, and yes the GUI should be frozen. – James Fazio Oct 30 '12 at 02:54
  • Better to place your updates inside a `javax.swing.Timer`, this will allow you update the UI within a fixed time interval without "stalling" the program – MadProgrammer Oct 30 '12 at 02:56
  • 1
    If you want to lock the GUI, use a `GlassPane` or `JXLayer`, pausing the EDT has more consequence then just "freezing" the UI, you can end up with out sequence repaints (the repaint manager may compress more of you updates into a single update while it's waiting for the EDT), resizing the window will not repaint properly – MadProgrammer Oct 30 '12 at 03:08

1 Answers1

1

If you want the updates to be spaced out, you've better of using something like a javax.swing.Timer. This will allow to schedule regular updates without causing the UI to look like it's crashed/hung.

enter image description here

This example will update the UI every 250 milli seconds

public class TestTimerUpdate {

    public static void main(String[] args) {
        new TestTimerUpdate();
    }

    public TestTimerUpdate() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException ex) {
                } catch (InstantiationException ex) {
                } catch (IllegalAccessException ex) {
                } catch (UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new TimerPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    protected class TimerPane extends JPanel {

        private int updates = 0;

        public TimerPane() {
            Timer timer = new Timer(250, new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    updates++;
                    repaint();
                }
            });
            timer.setRepeats(true);
            timer.setCoalesce(true);
            timer.start();
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            String text = "I've being updated " + Integer.toString(updates) + " times";
            FontMetrics fm = g2d.getFontMetrics();

            int x = (getWidth() - fm.stringWidth(text)) / 2;
            int y = ((getHeight() - fm.getHeight()) / 2) + fm.getAscent();

            g2d.drawString(text, x, y);

            g2d.dispose();
        }

    }

}

You can also have a look at How can I make a clock tick? which demonstrates the same idea

Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366