Skip to content

Instantly share code, notes, and snippets.

@AndresFRodriguezR
Forked from ajiniesta/DotDotLoading
Created April 13, 2017 05:37
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 AndresFRodriguezR/ed61314a83b77fa0bd19060eddac60dd to your computer and use it in GitHub Desktop.
Save AndresFRodriguezR/ed61314a83b77fa0bd19060eddac60dd to your computer and use it in GitHub Desktop.
DotDotLoading, represents the transitions of Text elements through JavaFX Services
import javafx.application.Application;
import javafx.application.Platform;
import javafx.concurrent.Service;
import javafx.concurrent.Task;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.text.Text;
import javafx.stage.Stage;
public class DotDotLoading extends Application{
public static void main(String[] args){
launch(args);
}
private Label label;
private Text dot = new Text("Loading.");
private Text dotdot = new Text("Loading..");
private Text dotdotdot = new Text("Loading...");
private HBox dots;
private Service<Void> serviceDots;
@Override
public void start(Stage stage) throws Exception {
VBox vbox = new VBox(10.0);
vbox.setAlignment(Pos.CENTER);
label = new Label("");
label.setVisible(true);
Button btn = new Button("Button");
btn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
runService();
}
});
dots = new HBox(10);
vbox.getChildren().addAll(btn, dots, label);
Scene scene = new Scene(vbox, 100, 100);
stage.setScene(scene);
stage.show();
}
protected void runService() {
final Service<Void> service = new Service<Void>() {
@Override
protected Task<Void> createTask() {
return new Task<Void>() {
@Override
protected Void call() throws Exception {
//Start the dots transitions
Platform.runLater(new Runnable() {
@Override
public void run() {
startDots();
}
});
//Execute the operation during the dots are running
longTask();
//Stop the dots transitions and show a label
Platform.runLater(new Runnable() {
@Override
public void run() {
stopDots();
label.setText("Task finished");
label.setVisible(true);
}
});
return null;
}
};
}
};
service.start();
}
private void startDots() {
serviceDots = new Service<Void>() {
@Override
protected Task<Void> createTask() {
return new Task<Void>() {
@Override
protected Void call() throws Exception {
int count = 0;
while(!isCancelled()){
Text toChoose = null;
int mod = count%3;
count++;
if(mod==0){
toChoose = dot;
}else if(mod==1){
toChoose = dotdot;
}else if(mod==2){
toChoose = dotdotdot;
}
final Text finalText = toChoose;
Platform.runLater(new Runnable() {
@Override
public void run() {
dots.getChildren().clear();
dots.getChildren().add(finalText);
}
});
Thread.sleep(1000);
}
return null;
}
};
}
};
serviceDots.start();
label.setVisible(false);
}
private void stopDots() {
serviceDots.cancel();
dots.getChildren().clear();
}
private void longTask() {
try{
Thread.sleep(10000);
}catch(Exception ex){
ex.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment