0

I have been struggling on this problem for a while so I thought I'd seek out some help.

I am creating an application for a game which requires me to make a dynamic countdown timer. By dynamic I mean being able to put your desired countdown timer in as the consumer. The problem I'm having is making my code wait 1000 milliseconds to execute the update code with the correct time. I am attempting to use the sleep functionality to do this...

This is not the application I am making this is just to simplify my problem down as much as I could for anyone willing to help me. Everything in here is directly from WindowBuilder in Eclipse IDE. The issue I'm having is getting the Thread thread = new Thread(); work with Thread.sleep(1000); for the full 1 second delay.

package test;

import java.awt.EventQueue;
import javax.swing.JFrame;
import java.awt.Color;
import javax.swing.JLabel;
import java.awt.BorderLayout;
import javax.swing.SwingConstants;
import java.awt.Font;

public class test {

private JFrame frame;

/**
 * 
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                test window = new test();
                window.frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}




/**
 * Create the application.
 */
public test() {
    initialize();
}

/**
 * Initialize the contents of the frame.
 */



private void initialize() {
    frame = new JFrame();
    frame.getContentPane().setBackground(Color.RED);

    JLabel lblNewLabel = new JLabel("Test");
    lblNewLabel.setFont(new Font("Tahoma", Font.PLAIN, 62));
    lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
    frame.getContentPane().add(lblNewLabel, BorderLayout.CENTER);
    frame.setBounds(100, 100, 450, 300);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    Thread thread = new Thread();

            for(int i = 60;i>=0;i--){
                thread.sleep(500);
                lblNewLabel.setText("Test" + i);
            }

}

}

If you throw this code in your IDE an error that reads Unhandled exception InterruptedException comes up. If I add in throws declarations the code just messes all up truthfully I don't know what the problem is there.

How can I fix or work around this?

Jakob991
  • 37
  • 1
  • 9
  • 2
    Don't use `Thread.sleep()` instead use a [Swing timer](https://docs.oracle.com/javase/tutorial/uiswing/misc/timer.html). See an [example](http://stackoverflow.com/a/34748083/2180785). Using `Thread.sleep()` will cause your GUI to freeze – Frakcool Jul 04 '16 at 01:06
  • @Rudrani Angira if i do that the jframe doest appear until after the 60 seconds i want the window to appear then update ever 60 seconds – Jakob991 Jul 04 '16 at 01:11
  • @Frakcool I will try that thank you – Jakob991 Jul 04 '16 at 01:11

1 Answers1

1

Here's another way to do it. Please note this code is not test.

private void initialize() {
    ...
    new Thread() {
        int counter = 10;
        public void run() {
            while(counter >= 0) {
                lblNewLabel.setText("Test" + (counter--));
                try{
                    Thread.sleep(1000);
                } catch(Exception e) {}
            }
        }
    }.start();
}
CMPS
  • 7,733
  • 4
  • 28
  • 53
  • Is there any way to name the thread? so i can make it start from a button click? @CMPS – Jakob991 Jul 04 '16 at 02:43
  • `Thread myThread = new Thread() { ... };` Then: `myThread.start();` but again try using a Swing Timer, it will be easier to implement and it's how you should accomplish this task. Above I posted a link with a timer that counts down from 3 to 0 and starts over (and there is a link to the original one that doesn't starts again). – Frakcool Jul 04 '16 at 03:40