Skip to content
November 23, 2009 / Narayan

Updated Date and Time in JavaFx (Bind)[]


 /* Class : DateTrack.fx
 
     Created on Nov 20, 2009, 3:35:25 PM
 */
 //=============================================================
 //This program will update the date of your current System Date
 //Now we are tracking date via help of UTILITY CLASS "DATE"
 //and we are taking advantage of the Timeline and init()
 //If any thing goes wrong then please comment me
 //==============================================================

package practice;  // you can change as per your package


//Import Directives...
import java.util.Date;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.text.Text;
import javafx.scene.text.Font;
import javafx.animation.Timeline;
import javafx.animation.KeyFrame;

/**
 * @author NARAYAN
 */
//--------------------
//Variable declaration
//--------------------

var myDate = "";  // just initializing..

//This will execute init() of Clock Class
var myClock = new Clock();

//Declaring Class Clock for updating Date
class Clock {
 public function nextTime() {
     var now = new Date();
     myDate = "{%tEc now}";
     /*
     * {%tEc now} is the extended format used
     * for the java.util.Date object ("now").
     *
     * %tx - Localized date
     * %tX - Localized time
     * %tu - A day of the week (1-7, 1 is for Monday)
     * %tEx - Locale's alternate date representation
     * %tEX - Locale's alternate time representation
     */

 }

 // THIS WILL BE EXECUTED AT FIRST...
 init { //INITIAL
     var time = Timeline {
         repeatCount: Timeline.INDEFINITE
         keyFrames: [
             KeyFrame {
                 time: 1s
                 action: function() {
                    nextTime();
                 }
             }
         ]
     }
     time.play(); // TAKING ACTION TO PLAY
 }
}

Stage {
     title: "Application title"
     width: 250
     height: 80
     scene: Scene {
         content: [
             Text {
                 font : Font {
                 size : 16
                }
             x: 10
             y: 30
             // Binding the current date
             // which helps to show updated date
             content: bind {myDate};

             }

         ]
     }
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: