4
Guys, I’ve been a few months with some stranded projects, because I can’t get a label
keep up to date.
I can make a stopwatch, but when it comes to the interface created by Scene Builder, it crashes because I could not update the label
, I think.
Please help me, I’m in great need.
Example:
package progrma.de.teste.nivel.pkg2;
import java.net.URL;
import java.util.ResourceBundle;
import java.util.Timer;
import java.util.TimerTask;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
public class interface2 implements Initializable {
@FXML
private Label label;
private int contador=0;
@FXML
private void handleButtonAction(ActionEvent event) {
Timer tm = new Timer();
tm.scheduleAtFixedRate(new TimerTask(){
@Override
public void run() {
contador++;
int seg = contador %60;
int min = contador /60;
int hora = min /60;
min %= 60;
label.setText(String.format("%02d:%02d:%02d:",hora,min,seg));
}
},1000,1000);
}
@Override
public void initialize(URL url, ResourceBundle rb) {
}
}
In the text where I spoke of the Service class it did not appear q it inherits from Service and the type of return. Ex: extends Service<void>
– giu_
It is another way to solve but here the main thing is to give an orientation. We use the Service class when there are changes in the UI code, which can only be made by FX thread and Task when the operations are in the background. Using Task favors isolating FX Thread for rendering for this reason I only used Task and Bind to solve this problem.
– Gustavo Fragoso
In addition, you can start a Task without inheritance by creating a thread and associating the task to it, thus: new Thread(task). start();
– Gustavo Fragoso
In the way you did the exercise, seeing the code and the way a Task was created, it was assumed that the code was done via programming. In the example q passed the exercise was done via FXML file. Therefore, I am assuming the fact q a Controller class was created. To complete my solution just instantiate an object of the Updadeservice class. ex: Updateservice service = new Updateservice() in the Controller class and in the initialize() method of the Controller class create a Bind type: label.textProperty(). bind(service.messageProperty());
– giu_
In the way you did the exercise, seeing the code and the way a Task was created, it is concluded that the code was done via programming. In the example q passed the exercise was done via FXML file. Therefore, I am assuming the fact q a Controller class was created. To complete my solution, simply instantiate an object of the Updadeservice class. ex: Updateservice service = new Updateservice() in the Controller class and in the initialize() method of the Controller class create a one-dimensional Bind type: label.textProperty(). bind(service.messageProperty());
– giu_
Also I would like to mention q, in the example q passed, of the Service class, the timer is set to wake up every 10 minutes. However, the way it is not working, you need to put another if in the time method after the 1º for type: if (Seconds == 30) { mediaPlayer.stop(); }. This way the timer will wake up every 10 minutes, or as each one wants to set up
– giu_