Table of contents
1.
Introduction
2.
Java Swing Program to create a Digital watch
2.1.
Algorithm
3.
Frequently Asked Questions
3.1.
What can you use Java Swing for?
3.2.
What is a digital watch?
3.3.
How to create a digital clock in Java?
3.4.
How do you create a StopWatch in Java?
4.
Conclusion
Last Updated: Mar 27, 2024
Medium

Digital Watch in Java Swing

Author GAZAL ARORA
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Java Swing is a Java GUI widget toolkit that comes with various useful widgets. It is a part of the Java Foundation Classes(JFC). JFC contains many packages for creating desktop applications.

We can also use Java Swing to create a Digital clock.

Java Swing Program to create a Digital watch

We will use the following Java packages:

  • swing package
  • awt package
  • A util package
  • The text package

Algorithm

  • To create a frame, we must first import the swing package. 
  • Then, the util package will be used to build the user interface. 
  • We also use a text package to write the program where date-time is required.
  • We first create a frame and define its dimensions. 
  • We then set the values of all the variables to zero and initialize the three variables Hours, Minutes, and Seconds.
  • In this software, the Calendar class is used. The abstract class that gives us access to date, time, year, and month methods is the Java Calendar class.
  • The millisecond interval is provided by Sleep(1000).
     
// Java program to create digital watch
import javax.swing.*;  
import java.awt.*;  
import java.text.*;  
import java.util.*;  


public class DigitalWatch implements Runnable{  


JFrame frame;  
Thread thread=null;  
int hours=0, minutes=0, seconds=0;  
String timeString = "";  
JButton button;  
  
DigitalWatch(){  
    frame=new JFrame();  
      
    thread = new Thread(this);  
        thread.start();  
      
    button=new JButton();  
        button.setBounds(100,100,100,50);  
      
    frame.add(button);  
    frame.setSize(300,400);  
    frame.setLayout(null);  
    frame.setVisible(true);  
}  
  
 public void run() {  
      try {  
         while (true) {  
  
            Calendar cal = Calendar.getInstance();  
            hours = cal.get( Calendar.HOUR_OF_DAY );  
            if ( hours > 12 )
            {hours -= 12;}  
            minutes = cal.get( Calendar.MINUTE );  
            seconds = cal.get( Calendar.SECOND );  
  
            SimpleDateFormat formatter = new SimpleDateFormat("hh:mm:ss");  
            Date date = cal.getTime();  
            timeString = formatter.format(date);  
  
            printTime();  
  
            thread.sleep(1000); //in milliseconds  
         }  
      }  
      catch (Exception e) { }  
 }  
  
  public void printTime(){  
    button.setText(timeString);  
  }  
  
  public static void main(String[] args) {  
    new DigitalWatch();  
  }  
}  
You can also try this code with Online Java Compiler
Run Code

 

The output of the program


Read also Swing components in java

Frequently Asked Questions

What can you use Java Swing for?

Java's Swing is a lightweight component of the Java foundation class. Applications with windows can be made using it. It includes components like scroll bars, 0 Buttons, text fields, and many other elements. A graphical user interface can be created by combining all these elements.

What is a digital watch?

A digital watch is defined as a watch that displays the time in numerical digits rather than by hands on a dial.

How to create a digital clock in Java?

Java's Thread and Graphics classes are used to create a digital clock. The Graphics class is used for the clock's design, and threads are used to change the clock's seconds, minutes, and hours.

How do you create a StopWatch in Java?

After creating an object of the library-provided StopWatch class stopWatch, we execute the start() function. We first call the Fibonacci() function to start the stopwatch and then use the stop() function to end it. We now call stopWatch to get the elapsed time.

Conclusion

In this article, we learned to program a digital clock in Java Swing.
I hope you all like it. If you want to learn more about Jawa Swing, refer to  Introduction to Java Swing.

Want to learn more but don’t know where to start?

Refer to our Guided Path on Coding Ninjas Studio to upskill yourself in Competitive ProgrammingData Structures and AlgorithmsJavaScriptSystem Design, and many more! To test your competency in coding, you can check out the mock test series and participate in various contests hosted on Coding Ninjas Studio! 

Happy Learning!

Live masterclass