Tomoya Suto
Tomoya Suto

Reputation: 1

How do I create a clock using Timer?

I am trying to create a seven segment display that automatically moves using the timer with the use of the ActionListener and actionPerformed.

I thought that if I use the for, if statement it would automatically loop from 0 to 2 and set the Background color for each segment of the numbers. However, when I display it, it is stuck on just displaying zero and will not count up from there.

Can anyone help me on what I am doing wrong, that makes this stuck on zero?

Here is the programming I have now using JFrame.

import javax.swing.Timer;

public class SevenSegment extends JFrame {
Timer timer = new Timer(100, null);

public SevenSegment() {
timer.start();

        timer.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                
                
                for (int i = 0; i < 3; i++) {
                    if(i == 0) {
                        lblNumberOne.setBackground(Color.red);
                        lblNumberTwo.setBackground(Color.red);
                        lblNumberThree.setBackground(Color.red);
                        lblNumberFour.setBackground(Color.red);
                        lblNumberFive.setBackground(Color.red);
                        lblNumberSix.setBackground(Color.red);
                    }
                    else if(i == 1) {
                        lblNumberTwo.setBackground(Color.red);
                        lblNumberThree.setBackground(Color.red);
                    }
                    else if(i == 2) {
                        lblNumberOne.setBackground(Color.red);
                        lblNumberTwo.setBackground(Color.red);
                
                    }
                        
                    
                }

          } 
            
        });
        
     }
}

Upvotes: 0

Views: 167

Answers (1)

Gilbert Le Blanc
Gilbert Le Blanc

Reputation: 51485

Here's about as simple a Swing GUI clock as you can make.

Clock GUI

I did this so I could show you a general way to start all of your Swing GUIs. Oracle has a helpful tutorial, Creating a GUI With JFC/Swing, that will show you how to create all kinds of Swing GUIs.

We start all Swing GUIs with a call to the SwingUtilities invokeLater method. This method ensures that all Swing components are created and executed on the Event Dispatch Thread.

We always create a JPanel to put our Swing components on. The only Swing component that we add to a JFrame is a JPanel or a JScrollPane. This allows us to separate the creation of a JFrame from the rest of the view. The JFrame code is nearly identical for all Swing applications. The only difference is the JPanels you add to the JFrame.

The JFrame code must be called in a specific order. This is the order I use for my Swing applications.

The JPanel code is in a separate paragraph for general tidiness. I like to keep things separate so I can focus on one small part of the GUI at a time.

The updateClockLabel method exists because I need to execute the code one time when I'm creating the JPanel, and five times a second thereafter, to actually update the JLabel.

By the way, I didn't write all this code (all 64 lines) in one shot. I wrote a little, tested a lot. My code was not correct the first time I wrote it, and I don't just mean making typos.

Here's the complete runnable code.

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;

public class SimpleClock implements Runnable {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new SimpleClock());
    }
    
    private JLabel clockLabel;

    @Override
    public void run() {
        JFrame frame = new JFrame("Clock");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        frame.add(createMainPanel(), BorderLayout.CENTER);
        
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
        
        Timer timer = new Timer(200, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent event) {
                updateClockLabel();
            }
        });
        timer.start();
    }
    
    private JPanel createMainPanel() {
        JPanel panel = new JPanel(new FlowLayout());
        panel.setBorder(BorderFactory.createEmptyBorder(5, 45, 5, 45));
        
        clockLabel = new JLabel(" ");
        clockLabel.setFont(panel.getFont().deriveFont(Font.BOLD, 72f));
        updateClockLabel();
        panel.add(clockLabel);
        
        return panel;
    }
    
    public void updateClockLabel() {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("H:mm:ss a");
        String timeDisplay = LocalTime.now().format(formatter);
        clockLabel.setText(timeDisplay);
    }

}

Upvotes: 3

Related Questions