In JavaFX, how can I display values which continuously change with time using "label" ?
You can change the text of a label using its setText() method. This can be done while the application is running. Here is an example of setting the text of a JavaFX Label: label.
A Text is a geometric shape (like a Rectangle or a Circle), while Label is a UI control (like a Button or a CheckBox). In Swing, geometric shapes were restricted to the painting mechanism, while in JavaFX they can be used in more generic ways. And you can use a Text clipping, giving the node shape by the text.
You can display multi-line read-only text in a Label . If the text has \n (newline) characters in it, then the label will wrap to a new line wherever the newline character is.
There are numerous ways to achieve that, the most convenient would be to use JavaFX's DataBinding mechanism:
// assuming you have defined a StringProperty called "valueProperty" Label myLabel = new Label("Start"); myLabel.textProperty().bind(valueProperty);
This way, every time your valueProperty gets changed by calling it's set method, the label's text is updated.
How about using SimpleDateFormat? There's no need for the StringUtilities class!
private void bindToTime() { Timeline timeline = new Timeline( new KeyFrame(Duration.seconds(0), new EventHandler<ActionEvent>() { @Override public void handle(ActionEvent actionEvent) { Calendar time = Calendar.getInstance(); SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm:ss"); setText(simpleDateFormat.format(time.getTime())); } } ), new KeyFrame(Duration.seconds(1)) ); timeline.setCycleCount(Animation.INDEFINITE); timeline.play(); } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With