Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Displaying changing values in JavaFx Label

Tags:

In JavaFX, how can I display values which continuously change with time using "label" ?

like image 945
Surjit Avatar asked Nov 05 '12 07:11

Surjit


People also ask

How do I change the value of a Label in JavaFX?

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.

What is the difference between Label and text in JavaFX?

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.

Can you display multiple lines of text in a Label JavaFX?

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.


2 Answers

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.

like image 73
zhujik Avatar answered Sep 18 '22 16:09

zhujik


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();  } } 
like image 41
Blindworks Avatar answered Sep 18 '22 16:09

Blindworks