Jack Jack - 1 year ago 78
Java Question

Update a clock using ActionListener - Java

I want a digital clock in Java displaying time and date, and the colons should blink. However, I can't make the colons blink. Here's my code:

import java.awt.Font;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.Timer;
import javax.swing.SwingConstants;
import java.util.*;
import java.text.*;

public class DigitalClock {

public static void main(String[] arguments) {

ClockLabel dateLable = new ClockLabel("date");
ClockLabel timeLable = new ClockLabel("time");
ClockLabel dayLable = new ClockLabel("day");

JFrame.setDefaultLookAndFeelDecorated(true);
JFrame f = new JFrame("Digital Clock");
f.setSize(300,150);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLayout(new GridLayout(3, 1));

f.add(dateLable);
f.add(timeLable);
f.add(dayLable);

f.getContentPane().setBackground(Color.black);

f.setVisible(true);
}
}

class ClockLabel extends JLabel implements ActionListener {
String type;
SimpleDateFormat sdf;
public ClockLabel(String type) {
this.type = type;
setForeground(Color.green);
Calendar calendar = Calendar.getInstance();
int seconds = calendar.get(Calendar.SECOND);
switch (type) {
case "date" : sdf = new SimpleDateFormat(" MMMM dd yyyy");
setFont(new Font("sans-serif", Font.PLAIN, 12));
setHorizontalAlignment(SwingConstants.LEFT);
break;
case "time" : if(seconds % 2 != 0) sdf = new SimpleDateFormat("hh:mm:ss a");
else sdf = new SimpleDateFormat("hh mm ss a");
setFont(new Font("sans-serif", Font.PLAIN, 40));
setHorizontalAlignment(SwingConstants.CENTER);
break;
case "day" : sdf = new SimpleDateFormat("EEEE ");
setFont(new Font("sans-serif", Font.PLAIN, 16));
setHorizontalAlignment(SwingConstants.RIGHT);
break;
default : sdf = new SimpleDateFormat();
break;
}
Timer t = new Timer(1000, this);
t.start();
}

public void actionPerformed(ActionEvent ae) {
Date d = new Date();
setText(sdf.format(d));
}
}


As you can see, I have the following lines:

case "time" : if(seconds % 2 != 0) sdf = new SimpleDateFormat("hh:mm:ss a");
else sdf = new SimpleDateFormat("hh mm ss a");


That way, the colons are visible when the seconds are odd, else the colons are not visible.

The problem is that if I launch the program and the second is odd at that time, then the colons are always visible. I don't understand why, since the second changes (time updates), but the colons don't.

Answer Source

i think you need to put this check

if(seconds % 2 != 0) 
  sdf = new SimpleDateFormat("hh:mm:ss a");
else 
  sdf = new SimpleDateFormat("hh mm ss a");

inside the actionPerformed method. thats the way you can switch the : on each sec.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download