Created
July 29, 2014 14:50
-
-
Save JaDogg/1dd0dba5eb2a621b8a3c to your computer and use it in GitHub Desktop.
JavaFX TimeLine
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<?import java.lang.*?> | |
<?import java.util.*?> | |
<?import javafx.scene.*?> | |
<?import javafx.scene.control.*?> | |
<?import javafx.scene.layout.*?> | |
<AnchorPane id="AnchorPane" prefHeight="111.0" prefWidth="266.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="FXMLTimeController"> | |
<children> | |
<TextField fx:id="txtTime" editable="false" layoutX="54.0" layoutY="38.0" /> | |
</children> | |
</AnchorPane> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.net.URL; | |
import java.util.Date; | |
import java.util.ResourceBundle; | |
import javafx.animation.KeyFrame; | |
import javafx.animation.Timeline; | |
import javafx.event.ActionEvent; | |
import javafx.event.EventHandler; | |
import javafx.fxml.FXML; | |
import javafx.fxml.Initializable; | |
import javafx.scene.control.TextField; | |
import javafx.util.Duration; | |
public class FXMLTimeController implements Initializable { | |
@FXML | |
private TextField txtTime; | |
//timeline | |
private Timeline timeline; | |
private void showTime() { | |
txtTime.setText((new Date()).toString()); | |
} | |
@Override | |
public void initialize(URL url, ResourceBundle rb) { | |
timeline = new Timeline(); | |
timeline.setCycleCount(Timeline.INDEFINITE); | |
timeline.setAutoReverse(false); | |
timeline.getKeyFrames().add( | |
new KeyFrame(Duration.seconds(1), | |
new EventHandler<ActionEvent>() { | |
@Override public void handle(ActionEvent event) { | |
showTime(); | |
} | |
})); | |
timeline.play(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javafx.application.Application; | |
import javafx.fxml.FXMLLoader; | |
import javafx.scene.Scene; | |
import javafx.scene.layout.Region; | |
import javafx.stage.Stage; | |
/** | |
* | |
* @author Bhathiya | |
*/ | |
public class TimeApp extends Application{ | |
@Override | |
public void start(Stage primaryStage) throws Exception { | |
Region root; | |
FXMLLoader loader = new FXMLLoader(this.getClass().getResource( | |
"FXMLTime.fxml")); | |
root = loader.load(); | |
Scene scene = new Scene(root); | |
primaryStage.setScene(scene); | |
primaryStage.show(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment