Skip to content

Instantly share code, notes, and snippets.

@sh9va
Created December 7, 2017 13:50
Show Gist options
  • Save sh9va/768049585e2eb8dbca487ebf9c5def84 to your computer and use it in GitHub Desktop.
Save sh9va/768049585e2eb8dbca487ebf9c5def84 to your computer and use it in GitHub Desktop.
JavaFX asynchronous multiple loaders at same time in a single view
import javafx.application.Application;
import javafx.concurrent.Task;
import javafx.concurrent.WorkerStateEvent;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ProgressIndicator;
import javafx.scene.control.TextArea;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class AsyncLoadingExample extends Application{
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
try {
HBox outerBox = new HBox(15) ;
//Left Side
VBox leftBox = new VBox(5);
Button leftButton = new Button();
leftButton.setText("Load Content from Server");
StackPane leftPane = new StackPane();
TextArea leftText = new TextArea();
leftPane.getChildren().add(leftText);
leftText.setText("Left Content");
leftButton.setOnAction(loadContent(leftPane,leftText));
leftBox.getChildren().addAll(leftButton, leftPane);
//Right Side
VBox rightBox = new VBox(5);
Button rightButton = new Button();
rightButton.setText("Load Content");
StackPane rightPane = new StackPane();
TextArea rightText = new TextArea();
rightText.setText("Right Content");
rightPane.getChildren().add(rightText);
rightButton.setOnAction(loadContent(rightPane,rightText));
rightBox.getChildren().addAll(rightButton, rightPane);
outerBox.getChildren().addAll(leftBox, rightBox);
Scene scene = new Scene(outerBox);
primaryStage.setScene(scene);
primaryStage.show();
}catch (Exception e) {
e.printStackTrace();
}
}
private EventHandler<ActionEvent> loadContent(StackPane pane,TextArea text) {
return new EventHandler<ActionEvent>() {
@Override public void handle(ActionEvent e) {
createTask(pane, text);
}
};
}
private void createTask(StackPane pane, TextArea text){
VBox loading = createProgressIndicator();
Task<String> task = new Task<String>(){
@Override
protected String call() throws Exception{
Thread.sleep(10000);
return " from Server";
}
};
Thread th = new Thread(task);
th.setDaemon(true);
th.start();
pane.getChildren().add(loading);
text.setDisable(true);
task.setOnSucceeded(new EventHandler<WorkerStateEvent>() {
@Override
public void handle(WorkerStateEvent t) {
String value = task.getValue();
text.setText(text.getText()+value);
pane.getChildren().remove(loading);
text.setDisable(false);
}
});
task.setOnFailed(new EventHandler<WorkerStateEvent>() {
@Override
public void handle(WorkerStateEvent evt) {
text.setText("Error while getting from server");
pane.getChildren().remove(loading);
text.setDisable(false);
}
});
}
private VBox createProgressIndicator() {
VBox loading = new VBox();
ProgressIndicator pi = new ProgressIndicator();
pi.setPadding(new Insets(0, 0, 0, 0));
loading.getChildren().addAll(pi);
loading.setAlignment(Pos.CENTER);
return loading;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment