1

I'm currently programming a mini game in java swing. I've got the GUI set up, and the game involves a sequence of numbers flashing up on screen and then disappearing - the user must then input the numbers again in the sequence they appeared.

When the numbers are initially displayed, I want them to display for 1-2 seconds, and then disappear, and have another number for 1-2 seconds etc.

However, I'm having issues with delaying the program whilst the number displays. I can't use Thread.sleep as it pauses the whole program with the hiding of previous numbers etc. It just doesn't work. I've tried every other suggestion I've come across, none of which have worked yet.

Anyone got anymore tips?

  • 1
    Show what you are attempting atm and probably what you tried with threads too – dave Dec 21 '17 at 18:40
  • 1
    Use a `Timer` object –  Dec 21 '17 at 18:43
  • https://docs.oracle.com/javase/tutorial/uiswing/misc/timer.html – JB Nizet Dec 21 '17 at 18:44
  • Have looked into a swing timer, but they seem to also pause the whole program like Thread.sleep does (i know they aren't suppose to...) have you got an example of one so I could look at it and modify? – Joe Maynard Dec 21 '17 at 18:50

2 Answers2

0

You can use Thread.sleep()

The problem you having is probably because you are trying to update the UI from Swing's event dispatching thread. This is a thread that is reserved for Swing components and you should do exactly nothing in it except quick updates to the UI.

public void prog() {
    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            label.setText("1");
        }
    }

    try {
        Thread.sleep(5000);
    } catch(Exception e) { }

    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            label.setText("2");
        }
    }
}

public static void main(String[] args) {
    label = new JLabel("0");

    prog();
}

JLabel label

The UI should remain responsive because of it's component interactions should be implemented in ActionListener's. But if you want to perform other work while waiting, or if the feature is contained in an ActionListener's actionPerfomed() method, you can kick off a new thread to sleep 5 seconds then update the UI. You could also perform some calculations that take 5 seconds to compute instead of sleeping without blocking the UI. The code would be:

(new Thread(new Runnable() {
    @Override
    public void run() {
        try {
            Thread.sleep(5000);
        } catch (Exception e) { }

        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                label.setText("2");
            }
        }
    }
}).start();
Abdul Ahad
  • 826
  • 8
  • 16
0
int delay = 5000; // delay in milliseconds 

ActionListener taskPerformer = new ActionListener() { 
    public void actionPerformed(ActionEvent evt) { //...Perform a task... } }; 
    Timer timer = new Timer(delay, taskPerformer);
    timer.setRepeats(false); 
    timer.start(); // timer starts - after delay time your task gets executed

Source

kalabalik
  • 3,792
  • 2
  • 21
  • 50
martin
  • 191
  • 7