How to show continuous time & date in Java


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: How to show continuous time & date in Java

  1. #1
    NewBie Guest

    How to show continuous time & date in Java


    I'd like to show time & date that adjust automatically as time passes by in
    my apps's Status Bar just like in windows taskbar.

    How to accomplish that in Java ?

    Thanks

    Setya

  2. #2
    Sciurus Guest

    Re: How to show continuous time & date in Java


    "NewBie" <jsetya@yahoo.com> wrote:
    >
    >I'd like to show time & date that adjust automatically as time passes by

    in
    >my apps's Status Bar just like in windows taskbar.
    >
    >How to accomplish that in Java ?


    Try this.

    TimeFrame.java:

    import java.awt.*;
    import java.awt.event.*;

    public class TimeFrame extends Frame {

    TextField timeField = new TextField();
    Button testButton = new Button();
    TextField testField = new TextField();
    FlowLayout flowLayout = new FlowLayout();
    TimeThread ticker;

    public TimeFrame(String title) {

    super(title);
    this.setSize(new Dimension(300, 200));
    this.setTitle("Timer");
    this.setLayout(flowLayout);
    this.setBackground(SystemColor.control);

    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    setLocation((screenSize.width - getSize().width)/2, (screenSize.height
    - getSize().height)/2);

    timeField.setColumns(32);
    timeField.setEnabled(false);

    testField.setColumns(32);
    testField.setEnabled(false);

    testButton.setLabel("Test");
    testButton.addActionListener(
    new ActionListener() {
    public void actionPerformed(ActionEvent ae) {
    testField.setText(timeField.getText());
    }
    }
    );

    this.add(timeField, null);
    this.add(testField, null);
    this.add(testButton, null);

    this.addWindowListener( new WindowAdapter() {
    public void windowClosing(WindowEvent e) {
    ticker.interrupt();
    setVisible(false);
    System.exit(0);
    }
    });

    setVisible(true);

    ticker = new TimeThread(this);
    ticker.start();

    }

    public static void main(String[] args) {
    TimeFrame myTimeFrame = new TimeFrame("Clock");
    }

    public void setTime(String time) {
    timeField.setText(time);
    }
    }

    TimeThread.java:

    public class TimeThread extends Thread {

    TimeFrame myTimeFrame;

    TimeThread(TimeFrame myTimeFrame) {
    this.myTimeFrame = myTimeFrame;
    }

    public void run() {
    boolean needRun = true;
    while (needRun) {
    myTimeFrame.setTime(new java.util.Date().toString());
    try {
    sleep(950);
    } catch (InterruptedException ie) {
    needRun = false;
    }
    }
    }
    }





  3. #3
    Setya Guest

    Re: How to show continuous time & date in Java


    "Sciurus" <sciurus@mail.zalaszam.hu> wrote:
    >
    >"NewBie" <jsetya@yahoo.com> wrote:
    >>
    >>I'd like to show time & date that adjust automatically as time passes by

    >in
    >>my apps's Status Bar just like in windows taskbar.
    >>
    >>How to accomplish that in Java ?

    >
    >Try this.
    >
    >TimeFrame.java:
    >
    >import java.awt.*;
    >import java.awt.event.*;
    >
    >public class TimeFrame extends Frame {
    >
    > TextField timeField = new TextField();
    > Button testButton = new Button();
    > TextField testField = new TextField();
    > FlowLayout flowLayout = new FlowLayout();
    > TimeThread ticker;
    >
    > public TimeFrame(String title) {
    >
    > super(title);
    > this.setSize(new Dimension(300, 200));
    > this.setTitle("Timer");
    > this.setLayout(flowLayout);
    > this.setBackground(SystemColor.control);
    >
    > Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    > setLocation((screenSize.width - getSize().width)/2, (screenSize.height
    >- getSize().height)/2);
    >
    > timeField.setColumns(32);
    > timeField.setEnabled(false);
    >
    > testField.setColumns(32);
    > testField.setEnabled(false);
    >
    > testButton.setLabel("Test");
    > testButton.addActionListener(
    > new ActionListener() {
    > public void actionPerformed(ActionEvent ae) {
    > testField.setText(timeField.getText());
    > }
    > }
    > );
    >
    > this.add(timeField, null);
    > this.add(testField, null);
    > this.add(testButton, null);
    >
    > this.addWindowListener( new WindowAdapter() {
    > public void windowClosing(WindowEvent e) {
    > ticker.interrupt();
    > setVisible(false);
    > System.exit(0);
    > }
    > });
    >
    > setVisible(true);
    >
    > ticker = new TimeThread(this);
    > ticker.start();
    >
    > }
    >
    > public static void main(String[] args) {
    > TimeFrame myTimeFrame = new TimeFrame("Clock");
    > }
    >
    > public void setTime(String time) {
    > timeField.setText(time);
    > }
    >}
    >
    >TimeThread.java:
    >
    >public class TimeThread extends Thread {
    >
    > TimeFrame myTimeFrame;
    >
    > TimeThread(TimeFrame myTimeFrame) {
    > this.myTimeFrame = myTimeFrame;
    > }
    >
    > public void run() {
    > boolean needRun = true;
    > while (needRun) {
    > myTimeFrame.setTime(new java.util.Date().toString());
    > try {
    > sleep(950);
    > } catch (InterruptedException ie) {
    > needRun = false;
    > }
    > }
    > }
    >}
    >

    Thanks a lot for the code

    BTW, I also need help to find out the On/Off status of CAPSLOCK and NUMLOCK
    key, so I can put that information in the status bar.

    Do you know how ?

    Any help would be greatly appreciated.

    Setya


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center