Syed Sadman
Syed Sadman

Reputation: 117

create a java gui countdown timer that starts with user input

Here is the github link

So I was trying to create an application and broke it down into 3 parts, one of which is to create a timer. This timer has two fields: one to input minutes and one to input seconds. It is supposed to take in minutes or seconds typed in by the user and display a countdown on the application screen. When the timer reaches 0, it alerts the user. I have searched everywhere and could not find a countdown timer in Java that does that. All I found were countdown timers that work in console or countdown timers that already have predefined values set by the developer, not user.

I wanted to make a timer just like this one from google: Google Timer

P.s. I am a new self-taught programmer. This is my second java project so I don't have much experience yet. Looking forward to any help :)

Here is my code:

public class TestPane extends JFrame {

private LocalDateTime startTime;
private JLabel timerLabel;
private Timer timer;
private JButton startbtn;
private JTextField timeSet;
int count;
int count_2;
private Duration duration;
private JTextField minSet;
private JTextField secSet;


public TestPane() {


    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setVisible(true);
    setSize(400,400);
    getContentPane().setLayout(new CardLayout(0, 0));

    JPanel panel = new JPanel();
    getContentPane().add(panel, "name_87856346254020");
    panel.setLayout(null);

    timerLabel = new JLabel("New label");
    timerLabel.setBounds(59, 48, 194, 14);
    panel.add(timerLabel);

    startbtn = new JButton("New button");
    startbtn.setBounds(124, 187, 89, 23);
    panel.add(startbtn);

    timeSet = new JTextField(1);
    timeSet.setBounds(106, 85, 86, 20);
    panel.add(timeSet);
    timeSet.setColumns(10);

    JLabel lblHours = new JLabel("Hours");
    lblHours.setBounds(29, 88, 46, 14);
    panel.add(lblHours);

    JLabel lblMinss = new JLabel("Mins");
    lblMinss.setBounds(29, 114, 46, 14);
    panel.add(lblMinss);

    JLabel lblSecs = new JLabel("Secs");
    lblSecs.setBounds(29, 139, 46, 14);
    panel.add(lblSecs);

    minSet = new JTextField(60);
    minSet.setBounds(75, 116, 86, 20);
    panel.add(minSet);
    minSet.setColumns(10);

    secSet = new JTextField();
    secSet.setBounds(75, 147, 86, 20);
    panel.add(secSet);
    secSet.setColumns(10);


    startbtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                 startbtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                timeSet.getText().trim();
                if(timeSet.getText().isEmpty()) {
                    duration = Duration.ofMinutes(count_min);
                } else {
                count_hour = Integer.parseInt(timeSet.getText());                   
                duration = Duration.ofHours(count_hour);                
                }

                minSet.getText().trim();                                
                if(minSet.getText().isEmpty()) {
                    duration = Duration.ofHours(count_hour);
                } else {
                    count_min = Integer.parseInt(minSet.getText());
                    duration = Duration.ofMinutes(count_min);                   
                }

                secSet.getText().trim();
                if(secSet.getText().isEmpty() && minSet.getText().isEmpty()) {
                duration = Duration.ofHours(count_hour);        
                } 
                else if(secSet.getText().isEmpty() && timeSet.getText().isEmpty()){
                    duration = Duration.ofMinutes(count_sec);
                }
                else {                  
                    count_sec = Integer.parseInt(secSet.getText());
                    duration = duration.ofSeconds(count_sec);
                }


                if (timer.isRunning()) {
                    timer.stop();
                    startTime = null;
                    startbtn.setText("Start");
                } 
                else {
                    startTime = LocalDateTime.now();
                    timer.start();
                    startbtn.setText("Stop");
                }

            }
        });           

        timer = new Timer(500, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                LocalDateTime now = LocalDateTime.now();
                Duration runningTime = Duration.between(startTime, now);
                Duration timeLeft = duration.minus(runningTime);
                if (timeLeft.isZero() || timeLeft.isNegative()) {
                    timeLeft = Duration.ZERO;
                    startbtn.doClick(); // Cheat
                }

                timerLabel.setText(format(timeLeft));
            }
        });
    }

    protected String format(Duration duration) {
        long hours = duration.toHours();
        long mins = duration.minusHours(hours).toMinutes();
        long seconds = (duration.minusMinutes(mins).toMillis() / 1000) %60;
        return String.format("%02dh %02dm %02ds", hours, mins, seconds);
    }   


    public static void main(String args[]){        
        new TestPane();
    }
}

Update: So far, when the user sets hours and leaves minutes and seconds blank, it counts down from hours. Same with seconds. However, I cannot get the minutes to do the same. If I leave everything blank except for minutes, the timer does nothing. Also, I want to make the timer work simultaneously with hours, minutes and seconds. As of now, it only works with one unit at a time.

enter image description here

Upvotes: 2

Views: 7843

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347194

So, the basic idea is to start with a Swing Timer. It's safe to use it to update the UI, won't block the UI thread and can repeat at a regular interval. See How to use Swing Timers for more details.

Because all timers only guarantee a minimum duration (that is, they could wait longer then the specified delay), you can't rely on updating a state value simply by adding the expected duration. Instead, you need to be able to calculate the difference in time between to points in time.

For this, I would use the Date/Time API introduced in Java 8. See Period and Duration and Date and Time Classes for more details.

From there, it's a simple matter of setting up a Timer, calculating the Duration from the start time to now and formatting the result

import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.time.Duration;
import java.time.LocalDateTime;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private LocalDateTime startTime;
        private JLabel label;
        private Timer timer;

        public TestPane() {
            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.insets = new Insets(2, 2, 2, 2);
            gbc.gridwidth = GridBagConstraints.REMAINDER;

            label = new JLabel("...");
            add(label, gbc);

            JButton btn = new JButton("Start");
            btn.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    if (timer.isRunning()) {
                        timer.stop();
                        startTime = null;
                        btn.setText("Start");
                    } else {
                        startTime = LocalDateTime.now();
                        timer.start();
                        btn.setText("Stop");
                    }
                }
            });
            add(btn, gbc);

            timer = new Timer(500, new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    LocalDateTime now = LocalDateTime.now();
                    Duration duration = Duration.between(startTime, now);
                    label.setText(format(duration));
                }
            });
        }

        protected String format(Duration duration) {
            long hours = duration.toHours();
            long mins = duration.minusHours(hours).toMinutes();
            long seconds = duration.minusMinutes(mins).toMillis() / 1000;
            return String.format("%02dh %02dm %02ds", hours, mins, seconds);
        }

    }


}

Countdown timer...

This timer counts up. How would I get it to count down instead? I tried putting in code from your links but it wouldn't work. The documentations didn't help me understand much either.

This requires a slightly better understanding of the Date/Time API.

Basically, you need to know the expected duration (how long the timer should run for) and the amount of time the timer has been running. From this, you can calculate the amount of time remaining (the countdown)

For simplicity, I started with a Duration of 5 minutes...

private Duration duration = Duration.ofMinutes(5);

Each time the Timer ticked, I simply calculated the running time and calculate the remaining time...

LocalDateTime now = LocalDateTime.now();
Duration runningTime = Duration.between(startTime, now);
Duration timeLeft = duration.minus(runningTime);
if (timeLeft.isZero() || timeLeft.isNegative()) {
    timeLeft = Duration.ZERO;
    // Stop the timer and reset the UI
}

All I really did was played around with the API. I knew I would need a Duration in the end (as that's what I was formatting) so I wanted to keep that. Since I also needed a "duration" of time, to represent the length of time the Timer should run for, the Duration class seemed like a good place to start.

I had thought I might need to calculate the difference between the two Durations (like I did for the runningTime), but as it turns out, all I really wanted was the difference between them (ie subtraction of one from the other).

All that was left was to add a check to ensure that the Timer doesn't run into negative time and you know have a concept of a "time out" or "count down" timer.

When you're dealing with these kinds of problems, its always a good thing to start out with a "core concept" of how it "might" work - ie, start by figuring out how you might count down in seconds. This gives you some ground work, from there, you can see what support the API provides and how you might use it to your advantage. In this case, Duration was super easy and continued to provide support for the formatting of the output

For example...

import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.time.Duration;
import java.time.LocalDateTime;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private LocalDateTime startTime;
        private JLabel label;
        private Timer timer;

        private Duration duration = Duration.ofMinutes(5);

        public TestPane() {
            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.insets = new Insets(2, 2, 2, 2);
            gbc.gridwidth = GridBagConstraints.REMAINDER;

            label = new JLabel("...");
            add(label, gbc);

            JButton btn = new JButton("Start");
            btn.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    if (timer.isRunning()) {
                        timer.stop();
                        startTime = null;
                        btn.setText("Start");
                    } else {
                        startTime = LocalDateTime.now();
                        timer.start();
                        btn.setText("Stop");
                    }
                }
            });
            add(btn, gbc);

            timer = new Timer(500, new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    LocalDateTime now = LocalDateTime.now();
                    Duration runningTime = Duration.between(startTime, now);
                    Duration timeLeft = duration.minus(runningTime);
                    if (timeLeft.isZero() || timeLeft.isNegative()) {
                        timeLeft = Duration.ZERO;
                        btn.doClick(); // Cheat
                    }

                    label.setText(format(timeLeft));
                }
            });
        }

        protected String format(Duration duration) {
            long hours = duration.toHours();
            long mins = duration.minusHours(hours).toMinutes();
            long seconds = duration.minusMinutes(mins).toMillis() / 1000;
            return String.format("%02dh %02dm %02ds", hours, mins, seconds);
        }

    }

}

Upvotes: 4

Related Questions