This doesn't change my label. Only first one runs and others don't change. In Java swing it worked but in Javafx doesn't.
package vehicalservice;
import com.jfoenix.controls.JFXProgressBar;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.application.Platform;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
/**
* FXML Controller class
*
* @author lahir
*/
public class SplashNewController implements Initializable {
@FXML
private StackPane stackPane;
@FXML
private AnchorPane anchorPane;
@FXML
private JFXProgressBar pbarLoad;
@FXML
private Label lblLoad = new Label();
/**
* Initializes the controller class.
*/
@Override
public void initialize(URL url, ResourceBundle rb) {
new SplashScreen().start();
}
class SplashScreen extends Thread{
@Override
public void run(){
try {
for (int i = 0; i <=100;i++){
Thread.sleep(40);
if(i<=30){
lblLoad.setText("Initilizing Components...");
}else if(i<=50){
lblLoad.setText("Initializing Database Connection...");
//new mainCLass().openDB();
}else if(i<=80){
lblLoad.setText("Initializing User Interface...");
}else{
lblLoad.setText("Please wait...");
}
}
Platform.runLater(new Runnable() {
@Override
public void run() {
Parent root=null;
try {
root = FXMLLoader.load(getClass().getResource("Login.fxml"));
Scene scene = new Scene(root);
Stage stage = new Stage();
stage.initStyle(StageStyle.UNDECORATED);
stage.setScene(scene);
stage.show();
stackPane.getScene().getWindow().hide();
} catch (IOException ex) {
//Logger.getLogger(SplashController.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
} catch (InterruptedException ex) {
//Logger.getLogger(SplashController.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
When you call Thread.sleep(40);
, you are actually stopping the JavaFX Application Thread and there will be no GUI updates. As it is pointed out in the comments, you should not suspend the application thread.
You could do the update from a different thread:
Task<Void> loadTask = new Task<Void>() {
@Override
protected Void call() throws Exception {
for (int i = 0; i <= 200; i++) {
Thread.sleep(40);
String text = "";
if (i <= 30)
text = "Initilizing Components...";
else if (i <= 50)
text = "Initializing Database Connection...";
else if (i <= 80)
text = "Initializing User Interface...";
else
text = "Please wait...";
String finalText = text;
Platform.runLater(() -> lblLoad.setText(finalText));
}
return null;
}
};
new Thread(loadTask).start();
You can use javafx.animation.Timeline
for periodic events:
int counter=0;
Timeline doEvery40sec = new Timeline(new KeyFrame(Duration.seconds(40), new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
if(counter==0)
lblLoad.setText("Initilizing Components...");
}else if(counter==1){
lblLoad.setText("Initializing Database Connection...");
}else if(counter==2){
lblLoad.setText("Initializing User Interface...");
}else if(counter==3)
lblLoad.setText("Please wait...");
counter++;
}
}));
fiveSecondsWonder.setCycleCount(4);
fiveSecondsWonder.play();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.