December 25, 2014

Create a clock using Java for your application. Clock will display on jlabel.

Create a clock using Java for your application

This code is to indicate system time on your application.This code is depending on your system time while startup the application and then it do feather calculations and counting to maintain accurate time.

This time function can simply  embed to a jlabel.
package default;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.Timer;
/**
*
* @author Open source developers
* Create a instance from this class and pass the jlabel which you desire to hold clock as a parameter to the constructor.
*/
public class Showclock {
public Showclock(final javax.swing.JLabel jlabel) {
final DateFormat timeFormat = new SimpleDateFormat("hh:mm:ss a ");
ActionListener timerListener = new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
Date date = new Date();
jlabel.setText(timeFormat.format(date));
}
};
Timer timer = new Timer(1000, timerListener);
timer.setInitialDelay(0);
timer.start();
}
}
//////////////// ENJOY JAVA SHARE JAVA /////////////////////////////////
view raw Showclock.java hosted with ❤ by GitHub

Related Posts:

0 comments:

Post a Comment

Leave your comment and feedback here for me