Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6607563
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T19:31:00+00:00 2026-05-25T19:31:00+00:00

This post relates to my last one regarding a timer. I decided the easiest

  • 0

This post relates to my last one regarding a timer. I decided the easiest thing to do for immediate results was to just write a Counter thread that counts down from a certain time (in this case 5 seconds) and if the counter reaches 0, the JFrame closes and let’s the user know that time has expired.

I’m running into some trouble, however. I cannot seem to make the JFrame close when the counter reaches 0. I’m not sure if I’m missing something stupid or if I am misunderstanding the way threads work and the way JFrames work. Here is the code, let me know what you think.

On a side note, I understand it would probably be most efficient to use a swing.Timer, but I just don’t quite grasp the nature of them yet. I’m under self-imposed time constraints (I’m not a student or anything, I just like to stay motivated) and so I’m “jerry-rigging” this thing for now.

Anyway, on to the code!

import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;

public class RacerDoom extends JFrame {

boolean timesUp=false;

public RacerDoom() {
//create JFrame
super("Racer Doom Squared");
setSize(WIDTH,HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
if(timesUp==true) {
dispose();
JOptionPane.showConfirmDialog(null, "Time's Up! Click Okay to try again!");
}

Counter c1 = new Counter();
c1.start();

//Counter
private class Counter extends Thread {
public Counter() {}
public void run() {
for(int i=5;i>=0;i--) {
if(i==0) {
timesUp=true;
}
System.out.println(i);
try{
Thread.sleep(1000);
}
catch(InterruptedException e){}
}
}
}
...

EDIT: I have the timer implemented and working. It does exactly what I need it to, but I can’t get the timer.stop(); command to work. I get the error “The local variable timer may not have been initialized.

Like I said, the timer works, it just never stops working until the program is terminated. Here is the constructor code for the JFrame, where the timer is located.

int counter = 0;

public RacerDoom() {
    //create JFrame
    super("Racer Doom Squared");
    setSize(WIDTH,HEIGHT);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setVisible(true);
    final Timer timer=new Timer(1000, new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            if(counter>=10) {
                timer.stop();  //the error occurs here
                dispose();
                JOptionPane.showConfirmDialog(null, "Time's Up!");
            }
            else{
               counter++;
               }
            System.out.println(counter);
        }
    });

    //inner thread
    Move1 m1 = new Move1();
    m1.start();

    timer.start();
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T19:31:01+00:00Added an answer on May 25, 2026 at 7:31 pm

    Thats easy to do with the help of a swing timer.. See this code sample:

    final java.swing.Timer timer=new Timer(1000, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(counter>5) {
                    timer.stop();
                    <dispose the fram here>
                }else{
                   counter++;
                   }
            }
        });
        timer.start();
    

    I put this code in the constructor of my JFrame which will run in the Event despatch thread. If you dont want hang up your GUI, make sure that you run this timer on another thread and when you are disposing the JFrame wrap the call with SwingUtilities.invokeLater() – This ensures that the call gets queued on the event despatch thread.

    I think your code is not working for the same reason, that you trying to something that does not get queued up in the event despatch thread. Here’s an article that will get you going

    http://java.sun.com/products/jfc/tsc/articles/threads/threads1.html

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This post relates to this: Add row to inlines dynamically in django admin Is
This relates to my previous post: jQuery .load Method causing page refresh AJAX I
This question is related with one of my earlier questions.. Previous Post In there
This post reference to the One Definition Rule. Wikipedia is pretty bad on explaining
This post relates to WordPress and CIMY User Extra Fields. I do not think
This question is related to a previous post of mine Here . Basically, I
This question is related to the post about having abstract at the titlepage. I
This question is related to the previous post. How to save file and read
This question is related to a previous post . Is there something comparable to
I looked at previous post based on this but they do not relate. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.