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 9274849
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T16:26:57+00:00 2026-06-18T16:26:57+00:00

I am trying to develop a game in an Applet and I am having

  • 0

I am trying to develop a game in an Applet and I am having this problem. I would like the display to show a countdown to the user before the game continues. However, the countdown will not display and is actually making the GUI freeze up instead. How can this be avoided? Here is some code demonstrating this problem.

EDIT: Code below ‘almost’ works, timer is going but screen will only update to new timer value whenever Start button is pressed. How can I make the text refresh automatically?

public class TestApplet extends JApplet implements ActionListener{


        final JTextField _displayField = new JTextField("Countdown", 6);
        CountDownTimer clock = new CountDownTimer();
        JButton jbtnStart = new JButton("Start");

    public void addComponentToPane(Container pane) {

        JPanel mainPanel = new JPanel();    
        mainPanel.add(jbtnStart);
        mainPanel.add(_displayField);
        pane.add(mainPanel);
        jbtnStart.addActionListener(this);
    }


  public void init() {

        TestApplet testApplet = new TestApplet();
        testApplet.setVisible(true);    
        testApplet.addComponentToPane(this.getContentPane());   
        this.setSize(200, 100);

}

    public void actionPerformed(ActionEvent e) {

      if ( e.getSource() == jbtnStart   ){
              clock.start(_displayField);
          }
   }     
}

// ********************************************************************************
//********************************************************************************
//********************************************************************************

class CountDownTimer  {

    private static final int N = 60;
    private final ClockListener cl = new ClockListener();
    private final Timer t = new Timer(1000, cl);
    static int count =0;

    public int getCount(){
         System.out.println(count);
        return count;
    }
    public void setCount(int n){
        count = n;
    }

    public CountDownTimer() {
        t.setInitialDelay(0);
    }

    public void start(JTextComponent c) {
        t.start();
       Boolean bool  = false;
          while ( bool ==false){     
              c.setText( "Starting new game in... "+ this.getCount() );
              bool = ( this.getCount()<10 );
          }
    }

    private class ClockListener implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            count %= N;
            count++;
           setCount(count);
        }
    }
}
  • 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-06-18T16:26:59+00:00Added an answer on June 18, 2026 at 4:26 pm

    You have a while loop in the ActionListener that is blocking the EDT. The code to update the display field should not be in the ActionListener.

    Instead this code should be in the Timer class. Then whenever the Timer fires you simply subtract one and update the display field. When the count reaches zero you stop the Timer.

    Also, your CountDownTimer should not extend a JFrame. It is just a class and has nothing to do with a frame.

    Edit:

    Here is a simple usage of a Swing Timer:

    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;
    import javax.swing.*;
    
    public class TimerTime extends JFrame implements ActionListener
    {
        JLabel timeLabel;
    
        public TimerTime()
        {
            timeLabel = new JLabel( new Date().toString() );
            getContentPane().add(timeLabel, BorderLayout.NORTH);
        }
    
        public void actionPerformed(ActionEvent e)
        {
            timeLabel.setText( new Date().toString() );
        }
    
        public static void main(String[] args)
        {
            TimerTime frame = new TimerTime();
            frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
            frame.pack();
            frame.setVisible(true);
    
            int time = 1000;
            javax.swing.Timer timer = new javax.swing.Timer(time, frame);
            timer.setInitialDelay(1);
            timer.start();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to develop a game and I was having issues managing the
I am trying to develop a simple game where a group of user can
I'm currently trying to develop a game using pygame and I'm having some problems
Hello everyone I'm trying to develop a game like Jetpack so i want to
I am trying to develop a game like fruit Slayer . I have code
i'm trying to develop Pentago -game in c#. right now i'm having 2 players
I am trying to develop a Java 2D game and I have a problem
Trying to develop a test app wherethe look is like ebook. user can flip
I would like to develop a game on Android platform, I have about a
Blarg! I'm trying to develop a game with zelda-like qualities. (i.e. When the PC

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.