September 04, 2010, 12:48:22 AM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  Home Help Media Affiliates Login Register  
Pages: [1]   Go Down
  Print  
Author Topic: Using threads to display the time.  (Read 5575 times)
0 Members and 1 Guest are viewing this topic.
Arkie
Javaforums.net Admin
Senior Member
*

Reputation: 16
Developer @ Javaforums.net
Offline Offline
Posts: 2620
Referrals: 13

WWW Awards
« on: July 10, 2007, 08:51:03 AM »

Code
GeSHi (java):
  1. import java.text.SimpleDateFormat;
  2. import java.util.Date;
  3.  
  4. import javax.swing.JFrame;
  5. import javax.swing.JLabel;
  6.  
  7. /*--------------------------------------------------------------------------
  8. //Author List:
  9. //   deAppel <Creator>
  10. //
  11. //Description:
  12. // My demo that displays the current hour/seconds using a Thread.
  13. //
  14. //Environment:
  15. //   This software was developed using Eclipse and Java 1.6
  16. //
  17. //Copyright Information:
  18. //   Copyright (C) 2007      <Institution><None>
  19. // Ark de Appel www.engineeringserver.com
  20. //
  21. //----------------------------------------------------------------------*/
  22.  
  23. public class TimeTicker extends JFrame implements Runnable{
  24. JLabel timelbl;
  25.  
  26. public TimeTicker(){
  27. //Container c = getContentPane();
  28. timelbl = new JLabel();
  29. add(timelbl);
  30. setSize(150,50);
  31. setResizable(false);
  32. setVisible(true);
  33. }
  34.  
  35. public void run(){
  36. for(;;){
  37. try {
  38. Thread.sleep(1000);
  39. } catch (InterruptedException e) {
  40. e.printStackTrace();
  41. }
  42. Date currentDate = new Date();
  43. SimpleDateFormat dateFormat = new SimpleDateFormat("h:mm:ss a");
  44. String d = dateFormat.format(currentDate);
  45. timelbl.setText("Current time " + d);
  46. this.setTitle("Time demo");
  47. }
  48. }
  49.  
  50. public static void main(String[] args){
  51. TimeTicker m = new TimeTicker();
  52. Thread t = new Thread(m);
  53. t.start();
  54. }
  55. }
  56.  
Created by GeSHI 1.0.7.20

Screenshot:

http://img258.imageshack.us/img258/2003/timelk0.png
Using threads to display the time.
Logged

Java and .NET developer

To students: It doesn't matter how hard you've studied; the material won't be on the exam anyway.

Fan of http://www.retardedweblogger.com
Oh man, too much stuff to do in so little time.

http://img222.imageshack....707/arkietomatoesmall.jpg
Blizzcon 2k9 Grubby and Cassandra Ng engaged ! <3
Triple D, eerste Denken Dan Doen
Javaforums.net :: a community about Java software development.
« on: July 10, 2007, 08:51:03 AM »

Your Ad Here
 Logged
deAppel
Favorite member
Member
*

Reputation: 0
HappyFace plants one on you
Offline Offline
Posts: 45
Referrals: 0

Awards
« Reply #1 on: July 16, 2007, 09:10:13 AM »

Second version: added a button that stops and start the time.


Code
GeSHi (java):
  1. import java.awt.Container;
  2. import java.awt.FlowLayout;
  3. import java.awt.event.ActionEvent;
  4. import java.awt.event.ActionListener;
  5. import java.text.SimpleDateFormat;
  6. import java.util.Date;
  7.  
  8.  
  9. import javax.swing.JButton;
  10. import javax.swing.JFrame;
  11. import javax.swing.JLabel;
  12.  
  13. /*--------------------------------------------------------------------------
  14. //Author List:
  15. //   deAppel <Creator>
  16. //
  17. //Description:
  18. // My demo that displays the current hour/seconds using a Thread.
  19. //
  20. //Environment:
  21. //   This software was developed using Eclipse and Java 1.6
  22. //
  23. //Copyright Information:
  24. //   Copyright (C) 2007      <Institution><None>
  25. // Ark de Appel www.engineeringserver.com
  26. //
  27. //----------------------------------------------------------------------*/
  28.  
  29. public class TimeTicker extends JFrame implements ActionListener, Runnable{
  30. JLabel timelbl;
  31. static Thread t;
  32. JButton startStop;
  33. boolean stop = true;
  34.  
  35. public TimeTicker(){
  36. Container c = getContentPane();
  37. c.setLayout(new FlowLayout());
  38. timelbl = new JLabel();
  39. startStop = new JButton("Stop clock");
  40. startStop.addActionListener(this);
  41. add(timelbl);
  42. add(startStop);
  43. setSize(200,100);
  44. //pack();
  45. setResizable(false);
  46. setVisible(true);
  47. }
  48.  
  49. public void run(){
  50. for(;;){
  51. try {
  52. Thread.sleep(1000);
  53. } catch (InterruptedException e) {
  54. e.printStackTrace();
  55. }
  56. Date currentDate = new Date();
  57. SimpleDateFormat dateFormat = new SimpleDateFormat("h:mm:ss a");
  58. String d = dateFormat.format(currentDate);
  59. timelbl.setText("Current time " + d);
  60. this.setTitle("Time demo");
  61. }
  62. }
  63.  
  64. public static void main(String[] args){
  65. TimeTicker m = new TimeTicker();
  66. t = new Thread(m);
  67. t.start();
  68. }
  69.  
  70. public void actionPerformed(ActionEvent arg0) {
  71. if(stop){
  72.  
  73. t.suspend();
  74.  
  75. startStop.setText("Start clock");
  76. stop = false;
  77. }
  78. else{
  79. t.resume();
  80. startStop.setText("Stop clock");
  81. stop = true;
  82. }
  83.  
  84. }
  85. }
  86.  
Created by GeSHI 1.0.7.20
Logged

Earn $0.04 or more for each search you make! http://www.myhpf.co.uk/apply001.asp?Friend=77889

If you need homework help, just post your assignment and i'll code it before your deadline =) i'll provide you "t3h c0d3z" but only if you show me that you have done something and not just ask for me to write everything for you. Just tell us what the problem is and i'm sure we can fix it for you!

I was born into the Hakka lineage, A bloodline that
traces it's roots back to the original Han emperors of
China. During the rise of the 3 Kingdoms (China's
famous warring period), the Hans were overthrown
and exiled. Forced to flee, they headed south finding
refuge within the mountains of Guilin Province where
they lived under their new title, the Hakka and patiently
wait for their chance to return to the throne as the
Sons Of Heaven...
AdnanAhsan
Member
*

Reputation: -1
Offline Offline
Posts: 15
Referrals: 0

Awards
« Reply #2 on: August 26, 2007, 05:33:39 AM »

very nice, did you test this code? please guide me about Runnable() and simpledateformat..
Logged
Arkie
Javaforums.net Admin
Senior Member
*

Reputation: 16
Developer @ Javaforums.net
Offline Offline
Posts: 2620
Referrals: 13

WWW Awards
« Reply #3 on: August 26, 2007, 05:46:25 AM »

very nice, did you test this code? please guide me about Runnable() and simpledateformat..

Runnable()
public interface Runnable

The Runnable interface should be implemented by any class whose instances are intended to be executed by a thread. The class must define a method of no arguments called run.

This interface is designed to provide a common protocol for objects that wish to execute code while they are active. For example, Runnable is implemented by class Thread. Being active simply means that a thread has been started and has not yet been stopped.

In addition, Runnable provides the means for a class to be active while not subclassing Thread. A class that implements Runnable can run without subclassing Thread by instantiating a Thread instance and passing itself in as the target. In most cases, the Runnable interface should be used if you are only planning to override the run() method and no other Thread methods. This is important because classes should not be subclassed unless the programmer intends on modifying or enhancing the fundamental behavior of the class.

http://java.sun.com/j2se/...i/java/lang/Runnable.html

SimpleDateFormat:
Code
GeSHi (java):
  1. Date currentDate = new Date();
  2. SimpleDateFormat dateFormat = new SimpleDateFormat("h:mm:ss a");
  3. String d = dateFormat.format(currentDate);
Created by GeSHI 1.0.7.20

Basically when you use SimpleDateFormat you first create a Date object and use the SimpleDateFormat to format your date as shown above.

Code
GeSHi (java):
  1. String d = dateFormat.format(currentDate);
Created by GeSHI 1.0.7.20
This line will display the date as: 11:42:59 AM (Hour:Minutes:Seconds Am/pm marker)

For more info:
http://java.sun.com/j2se/...ext/SimpleDateFormat.html
« Last Edit: August 26, 2007, 05:48:01 AM by HappyFace » Logged

Java and .NET developer

To students: It doesn't matter how hard you've studied; the material won't be on the exam anyway.

Fan of http://www.retardedweblogger.com
Oh man, too much stuff to do in so little time.

http://img222.imageshack....707/arkietomatoesmall.jpg
Blizzcon 2k9 Grubby and Cassandra Ng engaged ! <3
Triple D, eerste Denken Dan Doen
Javaforums.net :: a community about Java software development.
   

Your Ad Here
 Logged
Pages: [1]   Go Up
  Print  
 
Jump to:  

Your Ad Here
Powered by MySQL Powered by PHP Powered by SMF 1.1.10 | SMF © 2006-2009, Simple Machines LLC
TinyPortal v0.9.8 © Bloc
Valid XHTML 1.0! Valid CSS!
Page created in 0.402 seconds with 36 queries.

Google visited last this page September 01, 2010, 05:01:30 PM