MoistyB
Crypto Gaming Pro
2
MONTHS
2 2 MONTHS OF SERVICE
LEVEL 2
1000 XP
Introduction:
In this Tutorial, creation of clock which updates every second is explained. Here we create an Application for clock. It shows Hour, minutes and seconds which updates every second.
Main Idea:
The Dynamic clock can be created using Java's Timer thread but here we will see how to use our own thread and accomplish the task. This tutorial can clear any doubts you have in Java Threads. The Code which is to repeated or updated(the task) can be placed in Thread's run method.
Solution Description:
Step 1:
Firstly we have to create a class which extends JFrame in order to create an Application. We also need it to implement Runnable Interface in order to create and run Threads. Also WidowsListener Interface to enable closing the Application when window close button is pressed.
Step 2:
We need a Thread 'timer' which should be null initially. A JLabel to display the Clock text, a variable for storing date, a variable curr_Time to store the time to be displayed, it should be of String data type.
Step 3:
We create constructor of class in which we create a JPanel object and set FlowLayout to it and add a label to it.
Java's Date method returns date in long date format, to display the time in specific format we create a method formatTime() which returns formated time(String). To get current hour, minute, second, methods like getHour() of Date object are used.
To know whether the time is AM or PM, it is compared with 12. In order to display 12 hour time, 12 is subtracted from the time if it exceeds 12. concat() method of String class is used to concatenate hour, minute and second.
To display the time in “00:00:00” format padElement(int expr, char padchar) is used which pads the padchar('0') to the left if number is less than 10.
Step 4:
For the Dynamic clock the start() method initializes the timer Thread object and calls it's start() method. Stop() method stops the thread by making it null and finally the run() method is used to update it every second. Run() method tries to sleep for certain time and then runs a task. The task includes getting the current time by using Date object and then calling the formatTime function to get the time to be displayed in the specific format. This task runs while the timer Thread object is not null.
Step 5:
In the windowClosing() method of WindowListener, stop() method is called so that thread stops running and System's exit() method is called in order to exit the Application.
Step 6:
Finally in the main() method we have to create the object of the main class and call it's start() method to start the clock.
Complete code looks like below.
Download
In this Tutorial, creation of clock which updates every second is explained. Here we create an Application for clock. It shows Hour, minutes and seconds which updates every second.
Main Idea:
The Dynamic clock can be created using Java's Timer thread but here we will see how to use our own thread and accomplish the task. This tutorial can clear any doubts you have in Java Threads. The Code which is to repeated or updated(the task) can be placed in Thread's run method.
Solution Description:
Step 1:
Firstly we have to create a class which extends JFrame in order to create an Application. We also need it to implement Runnable Interface in order to create and run Threads. Also WidowsListener Interface to enable closing the Application when window close button is pressed.
Step 2:
We need a Thread 'timer' which should be null initially. A JLabel to display the Clock text, a variable for storing date, a variable curr_Time to store the time to be displayed, it should be of String data type.
Step 3:
We create constructor of class in which we create a JPanel object and set FlowLayout to it and add a label to it.
Java's Date method returns date in long date format, to display the time in specific format we create a method formatTime() which returns formated time(String). To get current hour, minute, second, methods like getHour() of Date object are used.
To know whether the time is AM or PM, it is compared with 12. In order to display 12 hour time, 12 is subtracted from the time if it exceeds 12. concat() method of String class is used to concatenate hour, minute and second.
To display the time in “00:00:00” format padElement(int expr, char padchar) is used which pads the padchar('0') to the left if number is less than 10.
Step 4:
For the Dynamic clock the start() method initializes the timer Thread object and calls it's start() method. Stop() method stops the thread by making it null and finally the run() method is used to update it every second. Run() method tries to sleep for certain time and then runs a task. The task includes getting the current time by using Date object and then calling the formatTime function to get the time to be displayed in the specific format. This task runs while the timer Thread object is not null.
Step 5:
In the windowClosing() method of WindowListener, stop() method is called so that thread stops running and System's exit() method is called in order to exit the Application.
Step 6:
Finally in the main() method we have to create the object of the main class and call it's start() method to start the clock.
Complete code looks like below.
- import
java.awt.Color
;
- import
java.awt.Container
;
- import
java.awt.FlowLayout
;
- import
java.awt.Font
;
- import
javax.swing.JFrame
;
- import
java.awt.event.WindowEvent
;
- import
java.awt.event.WindowListener
;
- import
java.util.Date
;
- import
javax.swing.JLabel
;
- import
javax.swing.UIManager
;
- import
javax.swing.UnsupportedLookAndFeelException
;
- public
class
Clock extends
JFrame
implements
Runnable
, WindowListener
{
- Thread
timer=
null
;
- String
dateToDisplay;
- int
hr;
- Date
d;
- JLabel
dateLabel=
new
JLabel
(
)
;
- int
hour;
- int
minute;
- int
second;
- String
amPm=
"AM"
;
- public
static
void
main(
String
[
]
args)
{
- Clock clock=
new
Clock(
)
;
- clock.setSize
(
200
, 80
)
;
- clock.setVisible
(
true
)
;
- try
{
- UIManager
.setLookAndFeel
(
UIManager
.getSystemLookAndFeelClassName
(
)
)
;
- }
- catch
(
ClassNotFoundException
e)
{
- }
catch
(
InstantiationException
e)
{
- }
catch
(
IllegalAccessException
e)
{
- }
catch
(
UnsupportedLookAndFeelException
e)
{
- }
- clock.setResizable
(
false
)
;
- clock.start
(
)
;
- }
- private
void
start(
)
{
- if
(
timer ==
null
)
- {
- timer =
new
Thread
(
this
)
;
- timer.start
(
)
;
- }
- }
- public
void
stop(
)
- {
- timer =
null
;
- }
- Clock(
)
- {
- this
.setLayout
(
new
FlowLayout
(
)
)
;
- dateLabel.setBackground
(
Color
.GRAY
)
;
- dateLabel.setForeground
(
Color
.MAGENTA
)
;
- dateLabel.setFont
(
new
Font
(
"Anklepants"
,Font
.PLAIN
,16
)
)
;
- this
.add
(
dateLabel)
;
- this
.setTitle
(
"Clock "
)
;
- this
.pack
(
)
;
- this
.setLocationRelativeTo
(
null
)
;
- }
- public
String
getFormatedDate(
Date
d)
- {
- String
formatedDate=
" "
;
- hour =
d.getHours
(
)
;
- minute =
d.getMinutes
(
)
;
- second =
d.getSeconds
(
)
;
- amPm=
(
hour<
12
)
?
"AM"
:
"PM"
;
- hr=
(
hour>
12
)
?
hour-
12
:
hour;
- formatedDate=
formatedDate.concat
(
padElement(
hr, '0'
)
)
;
- formatedDate=
formatedDate.concat
(
":"
)
;
- formatedDate=
formatedDate.concat
(
padElement(
minute, '0'
)
)
;
- formatedDate=
formatedDate.concat
(
":"
)
;
- formatedDate=
formatedDate.concat
(
padElement(
second, '0'
)
)
;
- formatedDate=
formatedDate.concat
(
" "
+
amPm)
;
- return
formatedDate;
- }
- private
String
padElement(
int
expr, char
padChar)
- {
- String
result =
""
;
- // I'm just padding 2 digit numbers
- if
(
expr <
10
)
result =
result.concat
(
String
.valueOf
(
padChar)
)
;
- result =
result.concat
(
String
.valueOf
(
expr)
)
;
- return
(
result)
;
- }
- public
void
run(
)
{
- // Sleep in the timer thread...
- while
(
timer !=
null
)
{
- try
{
timer.sleep
(
10
)
;
}
- catch
(
InterruptedException
e)
{
}
- d=
new
Date
(
)
;
- dateToDisplay=
getFormatedDate(
d)
;
- dateLabel.setText
(
dateToDisplay)
;
- }
- timer =
null
;
- }
- public
void
windowActivated(
WindowEvent
arg0)
{
- // TODO Auto-generated method stub
- }
- public
void
windowClosed(
WindowEvent
arg0)
{
- }
- public
void
windowClosing(
WindowEvent
arg0)
{
- stop(
)
;
- dispose(
)
;
- System
.exit
(
0
)
;
- }
- public
void
windowDeactivated(
WindowEvent
arg0)
{
- }
- public
void
windowDeiconified(
WindowEvent
arg0)
{
- }
- public
void
windowIconified(
WindowEvent
arg0)
{
- }
- public
void
windowOpened(
WindowEvent
arg0)
{
- }
- }
Download
You must upgrade your account or reply in the thread to view the hidden content.