Skip to content

Instantly share code, notes, and snippets.

@JaDogg
Created July 29, 2014 14:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JaDogg/1dd0dba5eb2a621b8a3c to your computer and use it in GitHub Desktop.
Save JaDogg/1dd0dba5eb2a621b8a3c to your computer and use it in GitHub Desktop.
JavaFX TimeLine
<?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>
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();
}
}
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