I am trying to create a countdown timer using the javax.swing.Timer class.
In order to display it I am only allowed to use javax.swing.JFrame and javax.swing.TextField classes.

Well my Timer works as I expected. But I have trouble when I am trying to display it in the JFrame. It appears that nothing is added on the JFrame.

Here is my code:

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

class CountdownTimer implements ActionListener {
    final int TOTAL_TIME = 10;
    int counter = TOTAL_TIME;
    javax.swing.Timer refreshTimer;
    //Creation of a JTextField
    JTextField countdownTimerField ;
    

    public CountdownTimer(JTextField f){
        refreshTimer = new javax.swing.Timer(1000, this);
        countdownTimerField = f;
    }


    

    public void actionPerformed(ActionEvent e) {
        counter--;
        if (counter >= 0){
            //System.out.println(" ** " + counter);
            countdownTimerField.setText(" ** " + counter);
        }
        if (counter == 0){
            //System.out.println("Blast off! ");
            countdownTimerField.setText(" Blast Off!");
            stop();
        }
    }

    public void start(){
        refreshTimer.start();
    }
    public void stop(){
        refreshTimer.stop();
    }
    public void reset(){
        counter = TOTAL_TIME;
    }
    
    public static void main(String[] args)
    {
        JTextField timerField = new JTextField();
        
        CountdownTimer cTimer = new CountdownTimer(timerField);
        
        JFrame frame = new JFrame("Countdown Timer");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(timerField);
        frame.pack();
        frame.setVisible(true);
    }
}

Dani AI

Generated

As pointed out, the root cause was that the javax.swing.Timer instance was never started. later confirmed that adding the missing start() call fixed the immediate symptom. Because the text field was left blank and the timer never fired, the window looked empty even though the component had been added.

A few practical fixes and hardening notes follow. Create and show the GUI on the Event Dispatch Thread (for example with SwingUtilities.invokeLater) so Swing behavior is predictable. Give the JTextField a sensible preferred width (e.g. new JTextField(8) or setColumns(8)) before calling pack() so the packed frame is large enough to show the field. Set an initial value in the field (for example the total time) before showing the frame so there is visible feedback immediately, and then call the timer's start() to begin updates.

Thread-safety and lifecycle: javax.swing.Timer posts events on the EDT, so updating the UI inside actionPerformed with setText is safe. If a non-Swing timer (for example java.util.Timer) is used instead, UI updates must be wrapped with SwingUtilities.invokeLater. Also check that reset() updates the displayed text (currently it only resets the counter) and decide whether reset() should restart the timer or leave it stopped.

Debugging tips: a zero-width field is a common cause of an “invisible” component — columns or an explicit preferred size solve that. revalidate()/repaint() can help after dynamic layout changes. With the missing start() added and the field sized/initialized, the countdown should display reliably.

Recommended Answers

All 3 Replies

It would go a lot better if you started your Timer!

Cheers my mistake!

Please mark this "solved" so people know not to worry.
J

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.